ビューで「time_ago_in_words」関数を使用しており、FunctionalTestで出力をテストする必要があります。
ただし、テストでは「time_ago_in_words」ヘルパー関数を確認できません。
FunctionalTestsのこれらのヘルパーメソッドを使用するにはどうすればよいですか?
ビューで「time_ago_in_words」関数を使用しており、FunctionalTestで出力をテストする必要があります。
ただし、テストでは「time_ago_in_words」ヘルパー関数を確認できません。
FunctionalTestsのこれらのヘルパーメソッドを使用するにはどうすればよいですか?
Include the ActionView::Helpers::DateHelper
module in your test_helper.rb
or test.rb
files. And that's it, from the test console:
>> time_ago_in_words(3.minutes.from_now)
NoMethodError: undefined method `time_ago_in_words' for #<Object:0x3b0724>
from (irb):4
from /Users/blinq/.rvm/rubies/ruby-1.9.1-p376/bin/irb:15:in `<main>'
>> include ActionView::Helpers::DateHelper
=> Object
>> time_ago_in_words(3.minutes.from_now)
=> "3 minutes"
I added on rails_helper.rb
config.include ActionView::Helpers::DateHelper
and work's, thank's @jpemberthy!
I had a similar issue recently where I was trying to access this function from an API so I wrapped it in a gem called timeywimey. Check it out: https://github.com/onetwopunch/timeywimey#usage
It allows you to access this helper from anywhere in a Rails project as well as Sinatra, and a native ruby project.
正確に何をテストしようとしていますか?time_ago_in_words
Rails独自のテストでカバーされているため、それ自体の動作を検証する必要はありません。を使用する独自のヘルパーの1つをテストしている場合はtime_ago_in_words
、ヘルパーテスト(から継承ActionView::TestCase
)で出力を確認できます。
機能テストは、コントローラーの動作(レンダリングするテンプレート、アクセス、リダイレクトなどを許可するかどうか)を検証することを目的としています。これには、特定のHTMLタグ(IDによる)の存在のチェックが含まれます。私は通常、タグの内容をチェックするためにそれらを使用することを避けようとします。