简体   繁体   English

Rails 3-如何测试控制器模块?

[英]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.

相关问题 如何在带有rspec的Rails控制器中测试关联? - How do I test association in rails controller with rspec? 在Rails中,如何测试在控制器中对Model实例所做的修改? - In Rails how do I test modifications to an instance of a Model that are made in a controller? 如何将模块混合到Rails 3控制器中 - How do you mixin a module into a Rails 3 controller Rails 集成测试:我的测试向控制器发布了一个新条目,如何检索该条目? - Rails integration test: my test posts a new entry to the controller, how do I retrieve this entry? 如何使用curl测试我的Rails控制器创建动作..还是可以使用rspec? - how do I use curl to test my Rails controller create action.. or can I use rspec? RSpec / Rails - 如何测试ApplicationController中的方法是否被调用(当我测试子类控制器时)? - RSpec / Rails - How do I test that a method in ApplicationController is called (when I'm testing a subclassed controller)? Rspec rails:如何测试一次控制器操作是否正确地使用修改后的参数重定向到其自身? - Rspec rails: How do I test if a controller action redirects correctly to itself with modified params once? 如何测试before_filter将为所有Rails控制器操作重定向? - How do I test that a before_filter will redirect for all Rails controller actions? 使用Rspec,如何在Rails 3.0.11中测试我的控制器的JSON格式? - Using Rspec, how do I test the JSON format of my controller in Rails 3.0.11? 如何在Rails集成/控制器单元测试中注销用户? - How do I log out a user in a Rails integration/controller unit test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM