Narazila jsem asi na drobnou chybu v tutoriálu Ruby on Rails od Michaela Hartla – v kapitole 8.5 Listing 8.23
NoMethodError: undefined method `full_title'
Po spuštění testu podle Listing 8.25 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.519295788] test_layout_links#SiteLayoutTest (0.52s) NoMethodError: NoMethodError: undefined method `full_title' for #<SiteLayoutTest:0x000000062eab48> test/integration/site_layout_test.rb:12:in `block in <class:SiteLayoutTest>' test/integration/site_layout_test.rb:12:in `block in <class:SiteLayoutTest>' 22/22: [=====================================================================] 100% Time: 00:00:00, Time: 00:00:00 Finished in 0.77941s 22 tests, 51 assertions, 0 failures, 1 errors, 0 skips
Řešení
Metoda full_title
je definována v modulu ApplicationHelper a je využívána v různých testech:
- test/integration/site_layout_test.rb,
- test/helpers/application_helper_test.rb
při volání metody full_title
.
Při úpravě souboru test/test_helper.rb dle Listing 8.23 došlo ke smazání řádku include ApplicationHelper
.
Původní kód
Listing 5.36 přidával do souboru test/test_helper.rb řádek include ApplicationHelper
.
ENV['RAILS_ENV'] ||= 'test' ... class ActiveSupport::TestCase fixtures :all include ApplicationHelper ... end
Nový kód
Podle Listing 8.23 již tam tento řádek není.
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 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 # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all include ApplicationHelper # Returns true if a test user is logged in. def is_logged_in? !session[:user_id].nil? end end
Spuštěný test je již GREEN
jitka@rails-tutorial:~/workspace/sample_app (log-in-log-out) $ bundle exec rake test Started 22/22: [=====================================================================] 100% Time: 00:00:00, Time: 00:00:00 Finished in 0.65962s 22 tests, 52 assertions, 0 failures, 0 errors, 0 skips