1

我正在尝试验证是否使用特定值调用了两次方法,但我似乎无法验证这两个调用,只是第一个调用。我已经验证了该方法被调用了两次并且值是正确的,但是我不确定如何编写雪松规范。

这是我所拥有的:

        it(@"should call sleep with time intervals of 0 and 5", ^{

            // subject is a spied on object
            subject should have_received(@selector(someMethod:)).with(0); // Passes
            subject should have_received(@selector(someMethod:)).with(5); // Fails
        }  

我得到的错误是:

Expected <MyObject> to have received message <someMethod:>, with arguments: <5> but received messages:
  someMethod:<0>
  someMethod:<5>
4

1 回答 1

0

我认为您实际遇到的问题是 Cedar 对类型非常挑剔。例如,假设someMethod:需要一个 NSTimeInterval,这就是您解决问题的方法。(如果不是 NSTimeInterval,则将其替换为实际类型)。

    it(@"should call sleep with time intervals of 0 and 5", ^{

        // subject is a spied on object
        subject should have_received(@selector(someMethod:)).with((NSTimeInterval)0);
        subject should have_received(@selector(someMethod:)).with((NSTimeInterval)5);
    }  

当您调用[subject someMethod:5]整数 5 时,它会从整数隐式转换为 NSTimeInterval,但是当您将整数 5 赋予 Cedar's 时,同样的事情不会发生with(),因此 Cedar 不会将它们视为相同。你的第一个断言通过只是因为它是 0。如果你将它更改为一个非零值,你会发现它就像第二个断言一样失败。

于 2015-05-29T23:45:19.810 回答