gpt4 book ai didi

objective-c - 在 Objective-C 中模拟指向指针的指针

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:13 26 4
gpt4 key购买 nike

我正在尝试模拟对 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 源代码之前,也许有人可以建议如何解决这个问题?如果您完全有更好的方法,则给予满分。

最佳答案

想通了。 By-reference 已经内置到 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]];

这看起来也更快乐!

关于objective-c - 在 Objective-C 中模拟指向指针的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15694136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com