17

ビューで「time_ago_in_words」関数を使用しており、FunctionalTestで出力をテストする必要があります。

ただし、テストでは「time_ago_in_words」ヘルパー関数を確認できません。

FunctionalTestsのこれらのヘルパーメソッドを使用するにはどうすればよいですか?

4

4 に答える 4

41

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"
于 2010-02-11T16:47:50.787 に答える
1

I added on rails_helper.rb

config.include ActionView::Helpers::DateHelper

and work's, thank's @jpemberthy!

于 2018-01-25T23:21:56.507 に答える
0

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.

于 2015-02-13T18:34:54.137 に答える
-1

正確に何をテストしようとしていますか?time_ago_in_wordsRails独自のテストでカバーされているため、それ自体の動作を検証する必要はありません。を使用する独自のヘルパーの1つをテストしている場合はtime_ago_in_words、ヘルパーテスト(から継承ActionView::TestCase)で出力を確認できます。

機能テストは、コントローラーの動作(レンダリングするテンプレート、アクセス、リダイレクトなどを許可するかどうか)を検証することを目的としています。これには、特定のHTMLタグ(IDによる)の存在のチェックが含まれます。私は通常、タグの内容をチェックするためにそれらを使用することを避けようとします。

于 2010-02-11T16:52:35.860 に答える