gpt4 book ai didi

ruby-on-rails - RSpec Controller 测试未生成正确的 url

转载 作者:数据小太阳 更新时间:2023-10-29 07:19:18 24 4
gpt4 key购买 nike

我正在尝试为命名空间 Controller 创建 RSpec Controller 测试,但 rspec 似乎无法检测嵌套并为 post :create 操作生成正确的路径。

这是我的规范代码:

# for: /app/controllers/admin/crm/report_adjustments_controller.rb
require 'spec_helper'
describe Admin::Crm::ReportAdjustmentsController do
render_views

before(:each) do
signin
end

describe "GET 'index'" do
it "returns http success" do
get :index
response.should be_success
end
end

describe "POST 'create'" do
it "creates with right parameters" do
expect {
post :create, report_adjustment: {distributor_id: @ole_distributor.id, amount: "30.0", date: Date.today }
}.to change(Crm::ReportAdjustment, :count).by(1)
response.should be_success
end
end
end

# routes.rb
namespace :admin do
namespace :crm do
resources :report_adjustments
end
end

对于这段代码,get :index 工作正常,但是当调用 post :create 时,会生成以下错误:undefined method 'crm_report_adjustment_url '

为什么 RSpec 足够聪明,可以用 get :index 解决问题,但不能用 post :create 解决问题?如何让 RSpec 正确加载正确的路由,即 admin_crm_report_adjustments_url

提前致谢。

最佳答案

尝试发布到 url:

post admin_crm_report_adjustments_url

# or

post "/admin/crm/report_adjustments"

关于ruby-on-rails - RSpec Controller 测试未生成正确的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16907994/

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