[英]Rails 3 - How do I test a controller module?
I have 3 controllers which include the same module. 我有3个包含相同模块的控制器。
How can I test with Test:Unit the module in one place ? 如何使用Test:Unit将模块放在一个地方进行测试?
Should I write 3 identical functional tests for each controller (not DRY) ? 我应该为每个控制器编写3个相同的功能测试(而不是DRY)吗?
TestController / TestController1 / TestController2 : TestController / TestController1 / TestController2:
class TestController < ApplicationController
include TestModule
test_module :test, :only => [:index]
...
end
TestModule : TestModule:
module TestModule
extend ActiveSupport::Concern
module ClassMethods
private
def test_module(resource, options = {})
self.before_filter(options.slice(:only, :except)) do
puts 'test_module'
end
end
end
end
Thanks in advance ! 提前致谢 !
Controller tests should test exactly that... the controller. 控制器测试应该准确地测试……控制器。 So any tests you're writing for the controller should verify that the controller actions do exactly what you expect, regardless of what modules you have included for them to do what they do. 因此,您为控制器编写的任何测试都应验证控制器动作是否完全符合您的期望,而不管您为它们执行的动作包含了什么模块。 Consider them as a black box, all you care about are giving the controller some inputs and then verifying the outputs are what they should be. 将它们视为黑匣子,您所关心的就是为控制器提供一些输入,然后验证输出是否应该是它们。
You should test the module as it's own entity, writing mock-ups and tests to verify each module method sufficiently. 您应该将模块作为自己的实体进行测试,编写模型和测试以充分验证每个模块方法。 Your controllers may change in the future, so this way you will always have test coverage for the module. 您的控制器将来可能会更改,因此,您将始终拥有该模块的测试范围。
You might want to learn to use standard tools such as RSpec for testing controllers. 您可能想学习使用标准工具(例如RSpec)来测试控制器。
Here's the link to Ryan Bates' Railscast 这是Ryan Bates的Railscast的链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.