我可以使用下面的代码来测试cruiser已经被调用了两次。但是如何测试第一次调用的参数是7,第二次调用的参数是8呢?
id cruiser = [Cruiser cruiser];
[[cruiser should] receive:@selector(energyLevelInWrapCore:) withCount:2];
[cruiser energyLevelInWarpCore:7];
[cruiser energyLevelInWarpCore:8];是否有可能在调用该方法后获取参数?如以下代码所示。
id cruiser = [Cruiser cruiser];
[cruiser stub:@selector(energyLevelInWarpCore:)];
[cruiser energyLevelInWarpCore:7];
[cruiser energyLevelInWarpCore:8];
[[[[[cruiser stub] calles][1] arguments][0] should] equal:theValue(8)]; // This doesn't work发布于 2013-06-16 19:10:46
你有真正的代码示例吗?在您给出的示例中,您已经将energyLevelInWarpCore:存根在测试的顶部,因此测试永远不会失败,因为您不会调用任何其他代码。您真正要做的就是练习测试框架。
假设您有一个只有一个WarpCore实例的Cruiser对象。向Cruiser发送消息engage应该启动warp内核,然后全速启动:
describe(@"Cruiser", ^{
describe(@"-engage", ^{
it(@"primes the warp core then goes to full speed", ^{
id warpCore = [WarpCore mock];
Cruiser *enterprise = [Cruiser cruiserWithWarpCore:warpCore];
[[[warpCore should] receive] setEnergyLevel:7];
[[[warpCore should] receive] setEnergyLevel:8];
[enterprise engage];
});
});
});Message patterns是测试参数的一种方式(您也可以使用receive:withArguments:)。上面的示例演示了为同一个选择器设置两个期望,但使用不同的参数,会产生两个唯一的断言。
您还可以在more complex scenarios, such as asynchronous code中使用Capture Spies测试参数。
https://stackoverflow.com/questions/17131333
复制相似问题