In rspec file, I am getting => Error: ActionController::RoutingError: No route matches xxxxxxx despite valid routing

In rspec file, I am getting => Error: ActionController::RoutingError: No route matches xxxxxxx despite valid routing

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

我有一个模型 recipe,它属于 sourcesource 属于 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"], "a$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

提前致谢。

好的!

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

错误的(旧的)语法(期望参数和会话明确作为参数):

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

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

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

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