如何测试块是否被调用
我有一个方法的单元测试,该方法应该调用完成块或失败块。现在我知道每种情况应该调用哪一个,因此我在不应调用的块中使用 STFail
。
我现在如何测试应该调用的块是否确实被调用?
这是我的设置:
NSString *parameter = @"foo";
[controller doSomethingWithParameter:parameter withcompletionBlock:
^(NSString *result)
{
// This block should be invoked
// Check if the result is correct
STAssertEquals(result, kSomeOKConstant, @"Result shout be 'kSomeOKConstant'");
} failedBlock:
^(NSString *errorMessage) {
STFail(@"No error should happen with parameter '%@'",parameter);
}];
I have a unit test for a method that should call either a completion block or a failed block. Now I know for every case which one should be called so I use STFail
in the block that should not be invoked.
How can I now test that the block that should be invoked is really invoked?
This is my setup:
NSString *parameter = @"foo";
[controller doSomethingWithParameter:parameter withcompletionBlock:
^(NSString *result)
{
// This block should be invoked
// Check if the result is correct
STAssertEquals(result, kSomeOKConstant, @"Result shout be 'kSomeOKConstant'");
} failedBlock:
^(NSString *errorMessage) {
STFail(@"No error should happen with parameter '%@'",parameter);
}];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要添加块变量,并从块内部设置它们:
此时,您可以对
successBlockInvoked
和failureBlockInvoked
的值进行断言:如果预期的值不是设置,你的测试失败了。You need to add block variables, and set them from inside your blocks:
At this point you can make assertions about the values of
successBlockInvoked
andfailureBlockInvoked
: if the expected one is not set, your test has failed.