我正试图模拟对NSURLConnection的调用.我调用我自己调用模拟对象的类方法调用.我正在尝试像这样实现模拟:
[[[[urlConnectionMock expect] andDo:^(NSInvocation *invocation) {
NSURLResponse * __autoreleasing *responsePointer;
[invocation getArgument:&responsePointer atIndex:3];
id response = [OCMockObject niceMockForClass:NSHTTPURLResponse.class];
[[[response stub] andReturnValue:OCMOCK_VALUE((NSInteger){ 200 })] statusCode];
*responsePointer = response;
}] andReturn:encryptedResponse] sendSynchronousRequest:OCMOCK_ANY returningResponse:OCMOCK_ANY error:OCMOCK_ANY];
问题是OCMOCK_ANY是一个Objective-C指针,而不是一个指向指针的指针(编译器正确地抱怨).在我深入研究OCMock源代码之前,也许有人可以建议如何解决这个问题?如果你有一个更好的方法,给予充分的信任.
最佳答案 弄清楚了.引用已经内置在OCMock中.
id fakeResponse = [OCMockObject niceMockForClass:NSHTTPURLResponse.class];
[[[fakeResponse stub] andReturnValue:OCMOCK_VALUE((NSInteger){ 200 })] statusCode];
[[[urlConnectionMock expect] andReturn:encryptedResponse] sendSynchronousRequest:OCMOCK_ANY returningResponse:[OCMArg setTo:fakeResponse] error:[OCMArg setTo:nil]];
这样看起来更加快乐!