测试替身:模拟与存根的使用指南
1. 测试数据库失败情况
在测试过程中,有时需要模拟数据库失败的情况,同时确保这种失败不会触发其他错误。下面是一个模拟 CreatesProject 工作流失败的测试示例:
mocks/01/spec/system/add_project_spec.rb
it "behaves correctly in the face of a surprising database failure" do
workflow = instance_spy(CreatesProject,
success?: false, project: Project.new)
allow(CreatesProject).to receive(:new)
.with(name: "Real Name",
task_string: "Choose Fabric:3\r\nMake it Work:5")
.and_return(workflow)
visit new_project_path
fill_in "Name", with: "Real Name"
fill_in "Tasks", with: "Choose Fabric:3\nMake it Work:5"
click_on("Create Project")
expect(page).to have_selector(".new_project")
end
这个测试的步骤如下:
1. 创
超级会员免费看
订阅专栏 解锁全文
25

被折叠的 条评论
为什么被折叠?



