我有一个旧的 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
运行此测试可以让我:
未初始化的常量 ActionDispatch::路由::路由(名称错误)
当我使用 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
我得到:
名称错误:未定义的局部变量或方法"_routes"为测试控制器:类
我错过了什么?我的测试帮助程序需要:
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
编辑:呃,对不起,这在 rails 3 上不起作用,但也许你已经升级了?
https://relishapp.com/rspec/rspec-rails/v/3-6/docs/controller-specs/engine-routes-for-controllers
您没有指定任何测试框架,所以我只想提到 RSpec 为您提供了type: :controller
和type: :routing
规范routes
方法,因此您可以执行以下操作
before do
routes.draw do
get ':controller/:action/:id' => 'controller#action'
end
end