Ruby on Rails tutorial - Listing 8.52

Kategorie: Programování » Ruby on Rails » Ruby on Rails tutorial - Listing 8.52

Datum vydání: 02. 04. 2015

Opět mi neprošel test v tutoriálu Ruby on Rails od Michaela Hartla – v kapitole 8.5 Listing 8.52.

NoMethodError: undefined method `full_title'

Po spuštění testu podle Listing 8.52 test nebyl zelený Green, ale červený Red. Objevila se mi následující hláška:

jitka@rails-tutorial:~/workspace/sample_app (log-in-log-out) $ bundle exec rake test
Started

ERROR["test_layout_links", SiteLayoutTest, 0.599118864]
 test_layout_links#SiteLayoutTest (0.60s)
NoMethodError:         NoMethodError: undefined method `full_title' for #<SiteLayoutTest:0x00000008205f28>
            test/integration/site_layout_test.rb:12:in `block in <class:SiteLayoutTest>'
        test/integration/site_layout_test.rb:12:in `block in <class:SiteLayoutTest>'

  25/25: [=====================================================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.69037s
25 tests, 59 assertions, 0 failures, 1 errors, 0 skips

Řešení

Řešení je shodné jako v článku Ruby on Rails tutorial - Listing 8.23. Je pouze třeba upravit kód uvedený v Listing 8.50 souboru test/test_helper.rb.

Původní kód

Listing 5.36 přidával do souboru test/test_helper.rb řádek include ApplicationHelper. Tento řádek tam nyní chybí.

ENV['RAILS_ENV'] ||= 'test'
...
class ActiveSupport::TestCase
  fixtures :all
  include ApplicationHelper
...
end

Nový kód

ENV['RAILS_ENV'] ||= 'test'
...
class ActiveSupport::TestCase
  fixtures :all

  # Returns true if a test user is logged in.
  def is_logged_in?
    !session[:user_id].nil?
  end

  # Logs in a test user.
  def log_in_as(user, options = {})
    password    = options[:password]    || 'password'
    remember_me = options[:remember_me] || '1'
    if integration_test?
      post login_path, session: { email:       user.email,
                                  password:    password,
                                  remember_me: remember_me }
    else
      session[:user_id] = user.id
    end
  end

  private

    # Returns true inside an integration test.
    def integration_test?
      defined?(post_via_redirect)
    end
end

Správný (funkční) kód

Aby testy prošly, tak je nutné ponechat řádek s include ApplicationHelper v kódu.

...
class ActiveSupport::TestCase
  fixtures :all
  include ApplicationHelper

  # Returns true if a test user is logged in.
  def is_logged_in?
    !session[:user_id].nil?
  end

  # Logs in a test user.
  def log_in_as(user, options = {})
    password    = options[:password]    || 'password'
    remember_me = options[:remember_me] || '1'
    if integration_test?
      post login_path, session: { email:       user.email,
                                  password:    password,
                                  remember_me: remember_me }
    else
      session[:user_id] = user.id
    end
  end

  private

    # Returns true inside an integration test.
    def integration_test?
      defined?(post_via_redirect)
    end
end

Spuštěný test je již GREEN

jitka@rails-tutorial:~/workspace/sample_app (log-in-log-out) $ bundle exec rake test
Started

  25/25: [=====================================================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.64914s
25 tests, 60 assertions, 0 failures, 0 errors, 0 skips