我有一个带有方法的Purchase
模型:
def set_status_to_in_progress!
self.update_attributes!(status: IN_PROGRESS)
end
失败的rspec测试:
context "self is invalid" do
it "raises an error" do
purchase = Purchase.new
purchase.stub(:valid?).and_return(:false)
expect { purchase.set_status_to_in_progress! }.to raise_error
end
end
返回
Failures:
1) Purchase#set_status_to_in_progress! self is invalid raises an error
Failure/Error: expect { purchase.set_status_to_in_progress! }.to raise_error
expected Exception but nothing was raised
# ./spec/models/purchase_spec.rb:149:in `block (4 levels) in <top (required)>'
我认为存根valid?
足以使ActiveRecord update_attributes!
方法引发错误?我该如何提高它?
答案 0 :(得分:3)
尝试更改:false为false
purchase.stub(:valid?).and_return(false)
或
purchase.should_receive(:valid?).and_return(false)
否则你可以存根任何购买实例
Purchase.any_instance.should_receive(:valid?).and_return(false)
答案 1 :(得分:0)
这是 DEFINITIVE 指南,用于在无法使用模型上的实际验证进行模拟时成功测试验证错误。在我的情况下,SupportRequest模型没有任何验证,但我想测试就像有一个,所以我先做的是创建一个double然后使它,所以它在尝试更新时返回false,然后添加错误到记录和最后测试记录在那里。 :)
describe "with invalid data" do
before do
the_double = instance_double("SupportRequest", id: support_request.id)
active_model_errors = ActiveModel::Errors.new(the_double).tap { |e| e.add(:description, "can't be blank") }
allow_any_instance_of(SupportRequest).to receive(:update_attributes).and_return(false)
allow_any_instance_of(SupportRequest).to receive(:errors).and_return(active_model_errors)
put "/api/support_requests/#{support_request.id}",
params: {
data: {
type: "support-requests",
attributes: {}
}
},
headers: authenticated_header(support_agent)
end
it "should not create a new support_request" do
expect_json(errors: [
{
source: {
pointer: "/data/attributes/description"
},
detail: "can't be blank"
}
])
end
it "should return status code (422)" do
expect_status(422)
end
end