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

发布于 2024-08-03 20:16:08 字数 1220 浏览 3 评论 0原文

我正在使用 Rails 2.2.2,想知道如何设置参数值来测试我的辅助方法。

我找到了一些示例,可以让您使用辅助方法运行测试,但当我直接在方法中使用请求或参数值时,它对我不起作用。

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

当使用请求或参数值时,我会收到一个错误,指出局部变量或方法未定义。我将如何设置该值?

使用请求值时应该出现错误,并且使用参数值时会出现相同的消息。

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`

I'm using rails 2.2.2 and wondering how can I set the params values to test my helper methods.

I found some examples to let you run tests with helper methods but it doesn't work for me when I use the request or params value directly in the method.

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

When using the request or params value I'll get an error that the local variable or method is undefined. How would I go about setting the value?

Error from shoulda when using the request value and it will be the same messages when using the params value.

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`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伪心 2024-08-10 20:16:08

我想您必须使用 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

我希望这会有所帮助:)

I guess you have to mock out calls to request and params using mocha or by defining mock objects in your test:

# 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

I hope this helps :)

冰葑 2024-08-10 20:16:08

这也有效:
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

This also works:
controller.params = {:filter => {:user_id => 1}

Here is an example:

require 'test_helper'

class BookmarksHelperTest < ActionView::TestCase

  def test_some_helper_method
    controller.params = {:filter => {:user_id => 1}
    #... Rest of your test
  end
end
み青杉依旧 2024-08-10 20:16:08

请注意,如果您使用 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

您甚至可以通过将 params 定义为 ActionView::TestCase 内部的方法来做得更好

As a note, if you are using Rails 2.3.x or anything which uses ActionView::TestCase - then all you really need to do is just have a private params method defined in your test.

e.g

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

You could even do one better by defining params as a method inside of ActionView::TestCase

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文