Jor*_*ell 3 rspec minitest ruby-on-rails-3
在从 RSpec 转换到 Minitest 时,我遇到了一个小问题,Google 没有提供任何帮助,那就是弄清楚如何做这样的事情:
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
Run Code Online (Sandbox Code Playgroud)
与 Minitest。有没有办法在 Minitest 内部创建这样的匿名控制器,或者是否有文档可以帮助我学习如何使用 minitest 测试控制器?
你可以这样做:
# 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1911 次 |
| 最近记录: |