是否可以在IOS中的XCTest单元测试用例中模拟一个方法(带参数)调用

时间:2015-08-28 21:47:35

标签: ios objective-c xctest

我在我的应用程序中使用第三方框架作为外部附件。在编写单元测试用例时,我需要绕过对库的所有直接调用。

//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

嘲笑我试过

方法1

 EXternalDeviceRequest * mockRequest = mock([EXternalDeviceRequest class]);
    id mockDevice = [OCMockObject mockForClass:[EXternalDevice class]];
    [[[mockDevice stub]andCall:@selector(accesoryRequestWithReference:error:) onObject:device]willReturn: mockRequest];

方法2

device = mock([EXternalDevice class]);
 EXternalDeviceRequest * mockRequest = mock([EXternalDeviceRequest class]);
[given([device accesoryRequestWithReference:@"" error:nil]) willReturn: mockRequest];

两者都不起作用。请帮忙。

提前致谢

1 个答案:

答案 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);