gpt4 book ai didi

ruby-on-rails-3 - Minitest w/Rails 中的匿名 Controller

转载 作者:行者123 更新时间:2023-12-01 07:44:24 24 4
gpt4 key购买 nike

在从 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

与 Minitest。有没有办法在 Minitest 中创建像这样的匿名 Controller ,或者是否有文档可以帮助我学习如何使用 minitest 测试 Controller ?

最佳答案

你可以这样做:

# 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

关于ruby-on-rails-3 - Minitest w/Rails 中的匿名 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12832909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com