简体   繁体   中英

Is it possible to mock a method(with parameters) call in XCTest unit test case in IOS

I am using a third party framework for external accessory in my application. While writing unit test case i need to bypass all the direct calls to the library.

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

Mocking i tried

Method 1

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

Method 2

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

Both are not working. Please help.

Thanks in advance

Try using OCMock's macro:

EXternalDeviceRequest * mockRequest = mock([EXternalDeviceRequest class]);
id mockDevice = [OCMockObject mockForClass:[EXternalDevice class]];
OCMStub([mockDevice accesoryRequestWithReference:[OCMArg any] error:[OCMArg setTo:nil]]).andReturn(mockRequest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM