定义帖子之前更容易理解的方式

| 我有一种方法可以在模拟对象上设置should_receive期望,但是这让我感到有些奇怪。
def mock_fax_event(stubs={})
  @mock_fax_event ||= mock_model(FaxEvent, stubs)
end

it \"should notify facility/admin of failed faxes\" do
  FaxEvent.should_receive(:find_by_fax_id).with(@fax_event.fax_id).and_return(mock_fax_event(:notify_failure => true))
  mock_fax_event.should_receive(:notify_failure)
  post :create, :TransactionID => @fax_event.fax_id
end
对我来说,我想做以下事情,但是不起作用:
it \"should notify facility/admin of failed faxes\" do
  post :create, :TransactionID => @fax_event.fax_id
  assigns(:fax_event).should_receive(:notify_failure)
end
我想我理解上述原因为何不起作用,但是我现在不清楚我的操作方式。我还想仅测试是否实际调用notify_failure,而不是find_by_fax_id部分。 有没有更好的方法可以做我想做的事情?
已邀请:
您的第二个示例不起作用,因为这是种鸡与蛋的问题。您要在
post
调用之后为对象设置期望值,这是导致该对象首先被分配的原因。而且您不能只交换行,因为
assigns
还没有任何返回值。 如果您不关心是否调用find_by_fax_id,则最好的方法是致电
FaxEvent.stub(:find_by_fax_id).and_return(...)
,但这并不好。 这是我喜欢使用Mocha的原因之一。你可以这样做:
FaxEvent.any_instance.expects(:notify_failure)
post :create, :TransactionID => @fax_event.fax_id
它使您可以跳过烦人的“查找我的模拟对象,而不是实际找到的对象”步骤。 同样,“ 6”违反命名约定,应为“ 7”。

要回复问题请先登录注册