我试图测试一个调用外部 API 的 create 方法,但我在模拟外部 API 请求时遇到了麻烦。这是我的设置以及到目前为止我尝试过的内容:
class Update
def self.create(properties)
update = Update.new(properties)
begin
my_file = StoreClient::File.get(properties["id"])
update.filename = my_file.filename
rescue
update.filename = ""
end
update.save
end
end
context "Store request fails" do
it "sets a blank filename" do
store_double = double("StoreClient::File")
store_double.should_receive(:get).with(an_instance_of(Hash)).and_throw(:sad)
update = Update.create({ "id" => "222" })
update.filename.should eq ""
end
end
目前我遇到了这个失败
Failure/Error: store_double.should_receive(:get).with(an_instance_of(Hash)).and_throw(:sad)
(Double "StoreClient::File").get(#<RSpec::Mocks::ArgumentMatchers::InstanceOf:0x000001037a9208 @klass=Hash>)
expected: 1 time
received: 0 times
为什么我的 double 不起作用以及如何最好地模拟对 的调用StoreClient::File.get
,以便我可以在 create 方法成功或失败时对其进行测试?