Wylliam Judd Wylliam Judd - 1 year ago 62
Ruby Question

route spec doesn't pass but route works

When I go to localhost:3000/api/guests/1 I get my show page, but when I run the spec, it doesn't pass.

Here's my spec:

it 'the resource is under the correct namespace' do
get '/api/guests/1'
expect(response).to be_success

Here's my routes.rb:

Rails.application.routes.draw do
namespace :api, defaults: { format: :json } do
resources :gifts, only: :show
resources :guests, only: [:index, :show] do
resources :gifts, only: :index
resources :parties, only: [:index, :show]

Here's my RSpec failure message:

Couldn't find Guest with 'id'=1

I'm not quite sure where to look for the problem.

gen gen
Answer Source

You did not create a Guest object, so your test fails because it can't find it. Before you call get, you should create your Guest. Something like:

user = Guest.create!(:username => "jdoe")

And then try to get it like this:

get "/api/guests/#{user.id}"

Note! Your test database is not the same as your development database. So, most likely you have that user in dev but not in test.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download