简体   繁体   中英

Rails 3 - How do I test a controller module?

I have 3 controllers which include the same module.

How can I test with Test:Unit the module in one place ?

Should I write 3 identical functional tests for each controller (not DRY) ?

TestController / TestController1 / TestController2 :

class TestController < ApplicationController
 include TestModule

 test_module :test, :only => [:index]
 ...
end

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.

Here's the link to Ryan Bates' Railscast

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM