我有一个课程如下......
class Sequencer
def initialize(error_raiser: nil)
@error_raiser = error_raiser || ErrorRaiser.new
end
def order(string)
error_raiser.raise_error(string)
end
attr_reader :error_raiser
end
我如何检查我的sequencer_spec文件中@error_raiser收到带字符串的raise_error?我试过像这样使用一个实例间谍......
let(:error_raiser) { instance_spy("ErrorRaiser" }
describe Sequencer do
describe '.order' do
it "passes the string to an error_raiser" do
subject.order("a => b,")
expect(error_raiser).to have_received(:raise_error).with("a => b")
end
end
end
但是我收到以下错误......
(InstanceDouble(ErrorRaiser) (anonymous)).raise_error("a => b")
expected: 1 time with arguments: ("a => b")
received: 0 times
答案 0 :(得分:0)
您应该告诉rspec您将其error_raiser替换为let
中定义的错误。
subject.stub(:error_raiser).and_return(error_raiser)
(取决于rspec版本)通常没有必要做一个实例间谍。以下应该有效
describe '.order' do
it "passes the string to an error_raiser" do
expect(subject.error_raiser).to receive(:raise_error).with("a => b")
subject.order("a => b")
end
end