我有 3 个包含相同模块的 Controller 。
如何使用 Test:Unit 将模块集中在一个地方进行测试?
我应该为每个 Controller 编写 3 个相同的功能测试(不是 DRY)吗?
测试 Controller /测试 Controller 1/测试 Controller 2:
class TestController < ApplicationController
include TestModule
test_module :test, :only => [:index]
...
end
测试模块:
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
提前致谢!
最佳答案
Controller 测试应该准确地测试…… Controller 。因此,您为 Controller 编写的任何测试都应该验证 Controller 的操作是否完全符合您的预期,无论您为它们包含了哪些模块来执行它们的操作。将它们视为一个黑盒子,您所关心的只是为 Controller 提供一些输入,然后验证输出是否正确。
您应该将模块作为其自身的实体进行测试,编写模型和测试以充分验证每个模块方法。您的 Controller 将来可能会发生变化,因此您将始终拥有模块的测试覆盖率。
关于ruby-on-rails - Rails 3 - 如何测试 Controller 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6940076/