私はrails 2.2.2を使用していて、ヘルパーメソッドをテストするためにparamsの値を設定するにはどうすればいいですか?Shouldaを使ってヘルパーメソッドをテストし、paramsとrequestの値を設定するにはどうすればよいですか?
ヘルパーメソッドでテストを実行できるようにした例がいくつか見つかりましたが、メソッドでrequestまたはparamsの値を直接使用するとうまくいきません。
require 'test_helper'
class ProductHelperTest < Test::Unit::TestCase
include ProductHelper
context 'ProductHelper' do
should 'build the link' do
assert_equal '', build_link
end
end
end
requestまたはparamsの値を使用すると、ローカル変数またはメソッドが定義されていないというエラーが表示されます。どのように値を設定するつもりですか?
要求値を使用するときはshouldaからのエラーで、params値を使用するときは同じメッセージになります。
1) Error:
test: ProductHelper should build the link. (ProductHelperTest):
NameError: undefined local variable or method `request` for #<ProductHelperTest:0x33ace6c>
/vendor/rails/actionpack/lib/action_controller/test_process.rb:471:in `method_missing`
/app/helpers/products_helper.rb:14:in `build_link`
./test/unit/product_helper_test.rb:10:in `__bind_1251902384_440357`
/vendor/gems/thoughtbot-shoulda-2.0.5/lib/shoulda/context.rb:254:in `call`
/vendor/gems/thoughtbot-shoulda-2.0.5/lib/shoulda/context.rb:254:in `test: ProductHelper should build the link. `
/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in `__send__`
/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in `run`
パラメータを渡せません。ビューからrs? – jonnii
私は既存のコードをテストしようとしており、ビューから値を渡さず直接使用します。ほとんどの場合、ビュー関数のパラメータ数を減らす可能性があります。 – Vizjerai