2009-09-02 3 views
1

私は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` 
+0

パラメータを渡せません。ビューからrs? – jonnii

+0

私は既存のコードをテストしようとしており、ビューから値を渡さず直接使用します。ほとんどの場合、ビュー関数のパラメータ数を減らす可能性があります。 – Vizjerai

答えて

3

私はあなたがmochaを使用してrequestparamsへの呼び出しをモックたり、テストでモックオブジェクトを定義することによってする必要が推測:

# Assuming ProductHelper implementation 
module ProductHelper 
    def build_link 
    "#{request.path}?#{params[:id]}" 
    end 
end 

class ProductHelperTest < Test::Unit::TestCase 
    include ProductHelper 

    # mock by defining a method 
    def params 
    { :controller => "test", :id => "23" } 
    end 

    # mock request.path using `mocha` # => "my_url" 
    def request 
    mock(:path => "my_url") 
    end 

    context 'ProductHelper' do 
    should 'build the link' do 
     assert_equal 'my_url?23', build_link 
    end 
    end 
end 

私はこれが役に立てば幸い:)

+0

ありがとう、このソリューションは完全に動作します。 paramsとrequest関数のために私が行った変更の1つは、@requestと@paramsを返すことでしたので、関数はクラスごとに一度しか再定義できないため、コンテキスト設定で返される内容を再定義できます。 – Vizjerai

+0

Vizjerai - これの例がありますか?私は試しました... @params = {}; def params; @params; end; ただし、[]エラーは発生しません。 –

0

注意点としてRails 2.3.xやActionView :: TestCaseを使用しているのであれば、本当に必要なことは、テストで定義されたprivate paramsメソッドを持つことだけです。

例えば

require 'test_helper' 

class BookmarksHelperTest < ActionView::TestCase 

    context "with an applied filter of my bookmarks" do 
    setup do 
     expects(:applied_filters).returns({:my_bookmarks => true}) 
    end 

    should "not be able to see it when other filters are called using my_bookmarks_filter" do 
     assert_equal other_filters(:my_bookmarks), {} 
    end 
    end 

    private 

    def params 
     {} 
    end 

end 

あなたもActionViewの内部方法としてのparamsを定義することによって、よりよいものを行うことができ:: TestCaseの

0

また、これは動作します:ここで controller.params = {:filter => {:user_id => 1}

は一例です:

require 'test_helper' 

class BookmarksHelperTest < ActionView::TestCase 

    def test_some_helper_method 
    controller.params = {:filter => {:user_id => 1} 
    #... Rest of your test 
    end 
end 
関連する問題