如何使用 Shoulda 测试辅助方法并设置参数和请求值?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想您必须使用
mocha
或通过在测试中定义模拟对象来模拟对request
和params
的调用:我希望这会有所帮助:)
I guess you have to mock out calls to
request
andparams
usingmocha
or by defining mock objects in your test:I hope this helps :)
这也有效:
controller.params = {:filter =>; {:user_id =>; 1}
这是一个示例:
This also works:
controller.params = {:filter => {:user_id => 1}
Here is an example:
请注意,如果您使用 Rails 2.3.x 或任何使用 ActionView::TestCase 的东西 - 那么您真正需要做的只是在测试中定义一个私有 params 方法。
例如,
您甚至可以通过将 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
You could even do one better by defining params as a method inside of ActionView::TestCase