This is my RSpec test:
describe "GET #show" do
it "assigns the requested user to @user" do
user = FactoryGirl.build(:user)
get :show, id: user
assigns(:user).should eq(user)
end
end
This is my rails controller:
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @user }
end
end
I get the following error message:
Failures:
1) UsersController GET #show assigns the requested user to @user
Failure/Error: get :show, id: user
ActiveRecord::RecordNotFound:
Couldn't find User without an ID
# /home/tim/fairym/app/controllers/users_controller.rb:25:in `show'
# ./users_controller_spec.rb:21:in `block (3 levels) in <top (required)>'
What is the proper way to use the get method here in order to make the test pass?
You should use FactoryGirl.create
and not build
. The reason is that create
actually makes an entry in the database, including ID. build
only makes an object in the memory, without an ID.