代码之家  ›  专栏  ›  技术社区  ›  Vizjerai

如何使用Shoulda测试助手方法并设置参数和请求值?

  •  1
  • Vizjerai  · 技术社区  · 16 年前

    我正在使用Rails2.2.2,想知道如何设置params值来测试我的助手方法。

    我找到了一些示例,可以让您使用helper方法运行测试,但当我直接在方法中使用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`
    
    3 回复  |  直到 16 年前
        1
  •  3
  •   splattael    16 年前

    我想你得假装打电话给我 request params 使用 mocha 或者通过在测试中定义模拟对象:

    # 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
    

        2
  •  0
  •   Omar Qureshi    15 年前

    请注意,如果您使用的是Rails 2.3.x或任何使用ActionView::TestCase的东西,那么您真正需要做的就是在测试中定义一个私有的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::TestCase内部的方法,您甚至可以做得更好

        3
  •  0
  •   user1094125    8 年前

    这也适用于: 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