抛出
使用 and_throw
使测试替身抛出提供的符号,可以选择性地提供参数。
* `and_throw(:symbol)`
* `and_throw(:symbol, argument)`
抛出符号
假设 一个名为“andthrowspec.rb”的文件,内容为
RSpec.describe "Making it throw a symbol" do
it "throws the provided symbol" do
dbl = double
allow(dbl).to receive(:foo).and_throw(:hello)
catch :hello do
dbl.foo
fail "should not get here"
end
end
it "includes the provided argument when throwing" do
dbl = double
allow(dbl).to receive(:foo).and_throw(:hello, "world")
arg = catch :hello do
dbl.foo
fail "should not get here"
end
expect(arg).to eq("world")
end
end
当 我运行 rspec and_throw_spec.rb
那么 示例应该全部通过。