Ruby on rails 3 带Rails的Minitest中的匿名控制器
在从RSpec转换到Minitest的过程中,我遇到了一个谷歌一点也帮不上的小问题,这就是如何做到这一点:Ruby on rails 3 带Rails的Minitest中的匿名控制器,ruby-on-rails-3,rspec,minitest,Ruby On Rails 3,Rspec,Minitest,在从RSpec转换到Minitest的过程中,我遇到了一个谷歌一点也帮不上的小问题,这就是如何做到这一点: describe ApplicationController do controller do def index render nothing: true end end it "should catch bad slugs" do get :index, slug: "bad%20slug" response.code.shou
describe ApplicationController do
controller do
def index
render nothing: true
end
end
it "should catch bad slugs" do
get :index, slug: "bad%20slug"
response.code.should eq("403")
end
end
用迷你测试。有没有办法在Minitest内部创建这样的匿名控制器,或者有没有文档可以帮助我学习如何使用Minitest测试控制器?我认为不支持匿名控制器。尝试在测试中定义控制器,而不是使用DSL创建控制器
class SlugTestController < ApplicationController
def index
render nothing: true
end
end
describe SlugTestController do
it "should catch bad slugs" do
get :index, slug: "bad%20slug"
response.code.must_equal "403"
end
end
class SlugTestController
您可以这样做:
# Add at runtime an action to ApplicationController
ApplicationController.class_eval do
def any_action
render :nothing
end
end
# If disable_clear_and_finalize is set to true, Rails will not clear other routes when calling again the draw method. Look at the source code at: http://apidock.com/rails/v4.0.2/ActionDispatch/Routing/RouteSet/draw
Rails.application.routes.disable_clear_and_finalize = true
# Create a new route for our new action
Rails.application.routes.draw do
get 'any_action' => 'application#any_action'
end
# Test
class ApplicationControllerTest < ActionController::TestCase
should 'do something' do
get :any_action
assert 'something'
end
end
#在运行时向ApplicationController添加操作
ApplicationController.class\u eval do
定义任何行动
渲染:没有
结束
结束
#如果disable_clear_和finalize设置为true,Rails在再次调用draw方法时将不会清除其他路由。查看以下位置的源代码:http://apidock.com/rails/v4.0.2/ActionDispatch/Routing/RouteSet/draw
Rails.application.routes.disable\u clear\u和\u finalize=true
#为我们的新行动创建新路线
Rails.application.routes.draw do
获取“任意动作”=>“应用程序任意动作”
结束
#试验
类ApplicationControllerTest
我想它不起作用了。您应该将测试中的路由指定为well@oivoodoo如果使用不存在的路由,则需要为测试用例创建临时路由。这可以通过和_routing
方法轻松完成。这是一个很好的例子。