gpt4 book ai didi

ruby-on-rails - FactoryGirl build_stubbed & RSpec - 生成 ID 但在测试 Show 操作时找不到 ID

转载 作者:数据小太阳 更新时间:2023-10-29 06:58:08 24 4
gpt4 key购买 nike

这是我的测试。我收到的错误是 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

Controller

def show
@profile = MedicalStudentProfile.find params[:id]
end

最佳答案

build_stubbed 不会将记录保存到数据库中,它只是为模型分配一个伪造的 ActiveRecord id 并 stub 数据库交互方法(如 save ),以便在调用它们时测试引发异常。尝试使用:

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

关于ruby-on-rails - FactoryGirl build_stubbed & RSpec - 生成 ID 但在测试 Show 操作时找不到 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31617657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com