FactoryGirl build_stubbed&RSpec - 生成ID但在测试Show动作时无法找到id

Wil*_*olt 7 ruby rspec ruby-on-rails factory-bot

这是我的测试.我得到的错误是ActiveRecord :: RecordNotFound:无法找到带有'id'= 1001的MedicalStudentProfile.我正确使用build_stubbed吗?

RSpec测试

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
Run Code Online (Sandbox Code Playgroud)

调节器

def show
 @profile = MedicalStudentProfile.find params[:id]
end
Run Code Online (Sandbox Code Playgroud)

iza*_*ban 8

build_stubbed不会将记录保存到数据库,它只是将错误的ActiveRecord id分配给模型并存根数据库交互方法(如save),以便在调用它们时测试会引发异常.尝试使用:

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

  • 谢谢.我检查了build_stubbed以加快控制器测试,但它似乎不适用于此处. (2认同)