如何在OCMock中模拟接受句柄作为参数的方法? [英] How do i mock a method that accepts a handle as an argument in OCMock?
问题描述
我正在尝试模拟一种等效于以下签名的方法:
I'm trying to mock a method that has the equivalent of the following signature:
- (NSDictionary *) uploadValues:(BOOL)doSomething error:(NSError **)error
我希望它返回一个小的字典,以便我的测试可以确保代码正确使用了字典.但是,无论我做什么,OCMock始终从该方法返回nil,无论我如何将其存根.错误开始于我正在测试的代码中的nil,这些是我尝试对其进行存根的不同方式:
I want it to return a small dictionary so that my test can make sure the code uses the dictionary properly. however, no matter what i do OCMock always returns nil from the method, regardless of how i stub it out. The error begins as nil in the code i'm testing, and these are the different ways i've tried stubbing it:
NSError * error = nil;
[[[mock stub] andReturn:someDict] uploadValues:YES error:&error];
[[[mock stub] andReturn:someDict] uploadValues:YES error:nil];
[[[mock stub] andReturn:someDict] uploadValues:YES error:[OCMArg any]];
,它们都不起作用. OCMock是否支持将消息参数作为存根处理?如果是的话,正确的处理方法是什么?
and none of them work. Does OCMock support handles as stubbed message arguments and if so, what's the correct way to do it?
推荐答案
遗憾的是,我也没有找到一个好的解决方案.我能说的最好的办法是尝试尽可能少地使用NSError **,然后将其放在一个隔离的函数中,您可以在部分模拟中完全模拟掉.
Sadly, I haven't found a good solution for this either. The best I can say is to try to make the use of the NSError** as small as possible, and then put it in an isolated function that you can completely mock out on your partial mock.
我发现,使用除NSObject *(或派生的)或原始值(NSInteger,BOOL等)之外的任何代码的代码几乎不可能使用OCMock进行测试.
I'm finding that any code that uses anything other than an NSObject* (or derived) or primitive values (NSInteger, BOOL, etc) is pretty much impossible to test using OCMock.
这篇关于如何在OCMock中模拟接受句柄作为参数的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!