gpt4 book ai didi

ruby-on-rails - 在 rspec 文件中,我得到 => Error: ActionController::RoutingError: No route matches xxxxxxx despite valid routing

转载 作者:行者123 更新时间:2023-11-28 21:35:37 27 4
gpt4 key购买 nike

我对测试和 Rails 非常陌生(不到 1 年的经验)。请在回答之前注意这一点。

我有一个模型 recipe 属于 source 并且 source 属于 client

路线是:

client_source_recipes GET/clients/:client_id/sources/:source_id/recipes(.:format) recipes#index

我正在尝试测试这个:

RSpec.describe RecipesController, :type => :controller do

# Prerequisites go here (mentioned at the end of the question)

describe "GET #index" do
it "assigns all recipes as @recipes" do
recipe = Recipe.create! valid_attributes
get :index, params: { client_id: client.id, source_id: source.id, locale: 'cs' }, session: valid_session
expect(assigns(:recipes)).to eq([recipe])

# Commented below are different ways I tried but failed:

# visit client_source_recipes_path(client.id, source.id, locale: 'cs')
# visit "/client/#{client.id}/source/#{source.id}/recipes?locale=cs"
# get client_source_recipes_path, params: { client_id: client.id, source_id: source.id, locale: 'cs' }, session: valid_session
# get client_source_recipes_path(client.id, source.id, locale: 'cs')
end
end
end

测试先决条件:

  login_user         # defined - works well

let(:client) { create :client } # defined in factories
let(:source) { create :source, warehouse_id: 1, client_id: client.id } # defined in factories

let(:valid_attributes) {
{ name: "name", source_id: source.id }
}

let(:valid_session) { {"warden.user.user.key" => session["warden.user.user.key"]} } # works well in other tests

当其他地方都在使用相同的路线时,为什么我会收到路线错误?

错误:

 Error: ActionController::RoutingError: No route matches {:controller=>"recipes", :params=>{:client_id=>"1", :source_id=>"1", :locale=>"cs"}, :session=>{"warden.user.user.key"=>[["1"], "$2a$04$bobnhLAlKEsxZtlJheY64."]}}

Error: ActionController::RoutingError: No route matches {:controller=>"recipes", :action=>"/clients/1/sources/1/recipes?locale=cs"}

Error: ActionController::RoutingError: No route matches {:controller=>"recipes", :action=>"/clients/1/sources/1/recipes"}

# etc. etc. i.e. errors are more or less the same

提前致谢。

最佳答案

好的!

我“花了”大约 5 个小时来思考这个问题,但无论如何都做不到,现在我发现唯一的问题是语法:

错误的(旧的)语法(显式期望参数和 session 作为参数):

get :index, { client_id: client.id, source_id: source.id, locale: 'cs' }, session: valid_session

#or

get :index, params: { client_id: client.id, source_id: source.id, locale: 'cs' }, session: valid_session

正确的(较新的)语法(隐式接受参数和 session 作为参数):

get :index, { client_id: client.id, source_id: source.id, locale: 'cs' }, valid_session

我希望它可以节省其他人的宝贵时间,不像我 ):

关于ruby-on-rails - 在 rspec 文件中,我得到 => Error: ActionController::RoutingError: No route matches xxxxxxx despite valid routing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59105522/

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