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)
build_stubbed不会将记录保存到数据库,它只是将错误的ActiveRecord id分配给模型并存根数据库交互方法(如save),以便在调用它们时测试会引发异常.尝试使用:
let!(:profile){create(:medical_student_profile)}
| 归档时间: |
|
| 查看次数: |
1822 次 |
| 最近记录: |