我在我的应用程序中使用第三方框架作为外部附件。在编写单元测试用例时,我需要绕过对库的所有直接调用。
//Actual Code i need to mock
ExternalDevice *device = [extern.devices firstObject];
// Able to mock this using "mock([ExternalDevice class])"
//myRequest is a request created using third party Library
self.myRequest = [device accesoryRequestWithReference:@"123456789" error:&error];
// I need to get a mock object of this request that i can pass from my test case
嘲笑我试过
EXternalDeviceRequest * mockRequest = mock([EXternalDeviceRequest class]);
id mockDevice = [OCMockObject mockForClass:[EXternalDevice class]];
[[[mockDevice stub]andCall:@selector(accesoryRequestWithReference:error:) onObject:device]willReturn: mockRequest];
device = mock([EXternalDevice class]);
EXternalDeviceRequest * mockRequest = mock([EXternalDeviceRequest class]);
[given([device accesoryRequestWithReference:@"" error:nil]) willReturn: mockRequest];
两者都不起作用。请帮忙。
提前致谢
答案 0 :(得分:0)
尝试使用OCMock的宏:
EXternalDeviceRequest * mockRequest = mock([EXternalDeviceRequest class]);
id mockDevice = [OCMockObject mockForClass:[EXternalDevice class]];
OCMStub([mockDevice accesoryRequestWithReference:[OCMArg any] error:[OCMArg setTo:nil]]).andReturn(mockRequest);