如何使用rspec测试这样的代码?

Foo.create! do |foo|
  foo.description = "thing"
end

我不想测试创建的对象——我想测试是否用正确的对象调用了正确的方法。相当于测试:
Foo.create!(description: "thing")

用这个:
Foo.should_receive(:create!).with(description: "thing")

最佳答案

这就是你要找的吗?

it "sets the description" do
  f = double
  Foo.should_receive(:create!).and_yield(f)
  f.should_receive(:description=).with("thing")

  Something.method_to_test
end

10-01 17:00
查看更多