如何使用 rspec 模拟 3.3 测试方法的救援块 [英] How do I test the rescue block of a method with rspec mocks 3.3
问题描述
这是一些rspec代码的例子,
Here is an example of some rspec code,
class User
attr_accessor :count
def initialize
@count = 0
end
# sometimes raises
def danger
puts "IO can be dangerous..."
rescue IOError => e
@count += 1
end
#always raises
def danger!
raise IOError.new
rescue IOError => e
@count += 1
end
end
describe User do
describe "#danger!" do
it "its rescue block always increases the counter by one" do
allow(subject).to receive(:'danger!')
expect {
subject.danger!
}.to change(subject, :count).by(1)
end
end
describe "#danger" do
context "when it rescues an exception" do
it "should increase the counter" do
allow(subject).to receive(:danger).and_raise(IOError)
expect {
subject.danger
}.to change(subject, :count).by(1)
end
end
end
end
我还创建了一个 fiddle,其中包含这些测试,所以你可以让他们通过.请帮我测试一个方法的救援块!
I've also created a fiddle with these tests in it, so you can just make them pass. Please help me test the rescue block of a method!
背景:
我最初的问题是这样的:
My original question went something like this:
我有一个方法,如下所示:
I have a method, like the following:
def publish!(resource)
published_resource = resource.publish!(current_project)
resource.update(published: true)
if resource.has_comments?
content = render_to_string partial: "#{ resource.class.name.tableize }/comment", locals: { comment: resource.comment_content_attributes }
resource.publish_comments!(current_project, published_resource.id, content)
end
true
rescue Bcx::ResponseError => e
resource.errors.add(:base, e.errors)
raise e
end
而且我想测试 resource.errors.add(:base, e.errors)
实际上是向资源添加错误.更一般地说,我想在一个方法中测试救援块.
And I want to test that resource.errors.add(:base, e.errors)
is, in fact, adding an error to the resource. More generally, I want to test the rescue block in a method.
所以我想写这样的代码,
So I'd like to write code like,
it "collects errors" do
expect{
subject.publish!(training_event.basecamp_calendar_event)
}.to change(training_event.errors.messages, :count).by(1)
end
当然,这会引发一个错误,因为我在救援区再次加注.
Of course, this raises an error because I am re-raising in the rescue block.
我已经看到一些使用旧的 something.stub(:method_name).and_raise(SomeException)
的答案,但 rspec 抱怨这种语法已被弃用.我想使用 Rspec Mocks 3.3 和 allow
语法,但我遇到了困难.
I've seen a few answers that use the old something.stub(:method_name).and_raise(SomeException)
, but rspec complains that this syntax is deprecated. I would like to use Rspec Mocks 3.3 and the allow
syntax, but I'm having a hard time.
推荐答案
我误解了 allow
语法的实际用途.所以为了让我的示例规范通过,我需要这样做:
I was misunderstanding what the allow
syntax is actually for. So to make my example specs pass, I needed to do this:
describe "#danger" do
context "when it rescues an exception" do
it "should increase the counter" do
allow($stdout).to receive(:puts).and_raise(IOError) # <----- here
expect {
subject.danger
}.to change(subject, :count).by(1)
end
end
end
我存根的这件事不是方法或主题,而是可能引发的对象.在这种情况下,我存根 $stdout
以便看跌期权将提高.
This thing that I'm stubing is not the method, or the subject, but the object that might raise. In this case I stub $stdout
so that puts will raise.
这是另一个fiddle,其中规范正在通过.
Here is another fiddle in which the specs are passing.
这篇关于如何使用 rspec 模拟 3.3 测试方法的救援块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!