Allow and Expect

Allow

用来mock某个方法的调用

allow(MyPlaceSNS).to receive(:publish).and_return(true)

Expect

期望某个方法被调用

expect(MyPlaceSNS).to have_received(:publish)

这里值得注意的是,我们希望MyPlaceSNS#publish被调用,但是在测试中,该方法又依赖了某些别的东西,所以如果是单纯的expect是不行的。我们需要先mock一下这个方法(第一句),然后再期望它被调用。

你可能感兴趣的:(Allow and Expect)