0

在使用 RSpec (2.10.0)、机械师 (2.0)、rspec-rails (2.10.1) 和 rails (2.3.6) 时,我在使用 RSpec::Mocks::ArgumentMatcher 时遇到问题anything

我有两个it使用匹配器的块,第一个成功,但第二个引发以下错误:

Failure/Error: Post.make!(:community => @community)
<Notification::Delayed (class)> received :create with unexpected arguments
expected: (#<RSpec::Mocks::ArgumentMatchers::AnyArgMatcher:0x00000104eff8f8>, #<User ...> , "event", "interval")
got: (#<Post>, <User...>, "event", "interval")

这些是 it 块(我已经改变了它们的顺序,它们的优先级,独立启动它们并且每个都在一次测试运行中),但仍然 1 号通过,2 号失败:

it "should create a delayed notification per default" do
  Notification::Email.should_not_receive(:notification)
  Notification::Delayed.should_receive(:create).with(anything, user, "event", "interval")
  Post.make!(:community => @community)
end

it "should create a delayed notification on user request" do
  NotificationSetting.make! :user => user, :event => 'event', :role => 'community,moderators', :interval => 'interval'
  Notification::Email.should_not_receive(:notification)
  Notification::Delayed.should_receive(:create).with(anything, user, 'event', 'interval')
  Post.make!(:community => @community)
end

为简单起见,我省略了为什么使用这个 any() 方法:这是因为作为第一个参数提交的对象可以是 Post 或 Post 的子类。考虑到这一点,我经历了所有其他 ArgumentMatchers (:duck_type, :kind_of? ...) 都没有运气。

我完全被卡住了,怎么会发生这种错误呢?“参数匹配器”部分中的文档不是说“参数可以是任何东西” - 那么怎么会有这样的错误?接下来让我毛骨悚然的是我对错误出现的无动于衷在第二种情况下不是在第一种情况下。至少不应该在两者上都失败吗?

很感谢任何形式的帮助

4

2 回答 2

0

你可以测试:

Notification::Delayed.should_receive(:create).with(kind_of(Post), user, 'event', 'interval')

Post 的子类也是 Post 的一种。

于 2012-07-31T18:08:44.097 回答
0

我无法解释,但我的 spec_helper 似乎有问题。在不同的项目中重现错误是不可能的。

对不起,打扰你。当我最终发现问题时,我会发布答案。

于 2012-08-04T20:53:17.773 回答