Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FactoryGirl build_stubbed & RSpec - Generates ID but fails to find id when testing Show action

Here is my test. The error I am getting is ActiveRecord::RecordNotFound: Couldn't find MedicalStudentProfile with 'id'=1001. Am I using build_stubbed correctly?

RSpec Test

RSpec.describe MedicalStudentProfilesController, type: :controller do

 let!(:profile){build_stubbed(:medical_student_profile)}
 let!(:user){build_stubbed(:user)}

 describe 'GET show' do

  it 'should show the requested object' do
   sign_in user
   get :show, id: profile.id
   expect(assigns(:profile)).to eq profile
 end
 end

end

Controller

def show
 @profile = MedicalStudentProfile.find params[:id]
end
like image 883
William Holt Avatar asked Jul 24 '15 18:07

William Holt


People also ask

What is Build_stubbed in Rspec?

build_stubbed is the younger, more hip sibling to build; it instantiates and assigns attributes just like build, but that's where the similarities end.

What is Build_stubbed?

build_stubbed is the younger, more hip sibling to build ; it instantiates and assigns attributes just like build , but that's where the similarities end.


1 Answers

build_stubbed doesn't save the record to the database, it just assigns a fake ActiveRecord id to the model and stubs out database interaction methods (like save ) such that the test raises an exception if they are called. Try using:

let!(:profile){create(:medical_student_profile)}

like image 137
izaban Avatar answered Dec 03 '22 15:12

izaban