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
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.
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.
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)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With