什么是嘲笑Rails 3条的路线和控制器插件测试的正确方法?
问题描述:
我有一个旧预Rails 3的插件,其测试下的Rails 3将不再运行测试看起来是这样的:什么是嘲笑Rails 3条的路线和控制器插件测试的正确方法?
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
运行这个测试使我:
uninitialized constant ActionDispatch::Routing::Routes (NameError)
当我使用with_routing,我认为在做测试路由的新方法,就像这样:
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
我得到:
NameError: undefined local variable or method `_routes' for TestController:Class
我错过了什么?我的测试助手要求:
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'
想法?谢谢!
答
你只需要利用Rails.application
路线Rails.application.routes.draw do
# Fun times
end
答
编辑:呃,抱歉,这不会在轨道上工作3,但也许你已经升级?
https://relishapp.com/rspec/rspec-rails/v/3-6/docs/controller-specs/engine-routes-for-controllers
您没有指定任何测试框架,所以我就提到,RSpec中为您提供了type: :controller
routes
方法和type: :routing
规格,所以你可以不喜欢
before do
routes.draw do
get ':controller/:action/:id' => 'controller#action'
end
end
东西记录:这种方法将重写所有应用程序的路线。所以要小心! – 2013-06-16 12:20:39