What's the proper way to mock Rails 3 routes and controllers in a plugin test?
I have an old pre-Rails 3 plugin whose tests will no longer run under Rails 3. The test looks something like this:
class TestController < ActionController::Base
def test_action; render :nothing => true; end
end
TestController.view_paths = [File.dirname(__FILE__)]
ActionController::Routing::Routes.draw {|m| m.connect ':controller/:action/:id' }
class TestControllerTest < ActionController::TestCase
context "test_action" do
should "do something" do
lambda { post :test_action }.should change { Model.count }
end
end
end
Running this test gets me:
uninitialized constant ActionDispatch::Routing::Routes (NameError)
When I use with_routing, which I thought was the new way of doing test routing, like so:
should "do something" do
with_routing do |set|
set.draw { |m| m.connect ':controller/:action/:id' }
lambda { post :test_action }.should change { Model.count }
end
end
I get:
NameError: undefined local variable or method `_routes' for TestController:Class
What am I missing? My tes开发者_运维百科t helper requires:
require 'rubygems'
require 'active_record'
require 'active_record/version'
require 'active_record/fixtures'
require 'action_controller'
require 'action_dispatch'
require 'action_view'
require 'test/unit'
require 'shoulda'
require 'mocha'
Ideas? Thanks!
You just need to draw the routes on Rails.application
Rails.application.routes.draw do
# Fun times
end
Edit: Uh, sorry, this won't work on rails 3, but maybe you upgraded already?
https://relishapp.com/rspec/rspec-rails/v/3-6/docs/controller-specs/engine-routes-for-controllers
You didn't specify any test framework, so I'll just mention that RSpec gives you routes
method in type: :controller
and type: :routing
specs so you could do something like
before do
routes.draw do
get ':controller/:action/:id' => 'controller#action'
end
end
精彩评论