Rails/rspec, integration test fail with "no route matches" but route does exist -


i doing simple integration testing. want test sold_items action in users controller. have confirmed route exists , returns json accessing browser. however, rspec telling me route doesn't exist. please see below, spec, error, , route.rb. thanks!

spec:

require 'rails_helper'  rspec.describe userscontroller, type: :controller    describe "get #items"     "returns http success"             user = factorygirl.create(:user)             "users/#{user.id}/sold_items"       expect(response).to have_http_status(:success)     end   end  end 

fail message:

  1) userscontroller #items returns http success      failure/error: "users/#{user.id}/sold_items"       actioncontroller::urlgenerationerror:        no route matches {:action=>"users/10/sold_items", :controller=>"users"}      # ./spec/controllers/users_controller_spec.rb:8:in `block (3 levels) in <top (required)>' 

routes.rb

rails.application.routes.draw       resources :users     resources :items     "users/:id/sold_items" => "users#sold_items" 

edit

spec

require 'rails_helper'  rspec.describe userscontroller, type: :controller    describe "get #items"     "returns http success"             user = factorygirl.create(:user)             "users/:id/sold_items", id: user.id        expect(response).to have_http_status(:success)     end   end  end 

failure message

  2) userscontroller #items returns http success      failure/error: "users/:id/sold_items", id: user.id       actioncontroller::urlgenerationerror:        no route matches {:action=>"users/:id/sold_items", :controller=>"users", :id=>"12"}      # ./spec/controllers/users_controller_spec.rb:8:in `block (3 levels) in <top (required)>' 

edit.2 marvc second suggestion

require 'rails_helper'

rspec.describe userscontroller, type: [:request, :controller]    describe "get #items"     "returns http success"             user = factorygirl.create(:user)             "/users/:id/sold_items", id: user.id         expect(response).to have_http_status(:success)     end   end  end 

failure

looks user.id isn't being passed in here

  2) userscontroller #items returns http success      failure/error: @sold_items = user.find(params[:id]).seller_items.sold       activerecord::recordnotfound:        couldn't find user 'id'=:id      # ./app/controllers/users_controller.rb:6:in `sold_items'      # ./spec/controllers/users_controller_spec.rb:8:in `block (3 levels) in <top (required)>' 

one solution: when use string interpolation pass user.id url works...

require 'rails_helper'  rspec.describe userscontroller, type: [:request, :controller]    describe "get #items"     "returns http success"             user = factorygirl.create(:user)             "users/#{user.id}/sold_items", id: user.id         expect(response).to have_http_status(:success)     end   end  end 

it seems syntax wrong.

change: get "users/#{user.id}/sold_items"

to:

rails 4

get "users/:id/sold_items", id: user.id

rails 5
get "users/:id/sold_items", params: { id: user.id }


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -