Reputation: 5049
I want to mock an array which is private property inside my class. i have made it available in my unit test by doing this. (this is inside my unit test file)
@interface MyViewController ()
@property (nonatomic, strong) NSArray myArray;
@end
Lets assume i have a type called Person
and this array should contains person objects. So i am doing the following in my test case
- (void)testBeneficiariesCount {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
id mockArray = OCMClassMock([NSArray class]);
self.myVC.myarray = mockArray;
Person *p1 = [[Person alloc] init];
Person *p2 = [[Person alloc] init];
Person *p3 = [[Person alloc] init];
Person *p4 = [[Person alloc] init];
Person *p5 = [[Person alloc] init];
p1.name = @“Alice"; p2.name = @“James”; p3.name = @“Oscar"; p4.name = @“Harri”; p5.name = @“John”;
persons = [NSArray arrayWithObjects:p1,p2,p3,p4,p5,nil];
OCMStub([self.myVC myArray]).andReturn(persons);
XCTAssertEqual([self.myVC numberOfPersons], 5);
}
myVC has a method named numberOfPersons
, when i run this, test case is failing complaining that (0) is not equal to (5)
. This means that i am not successfully able to mock my array, as i also tries to print the mocked array and it has nothing in it.
Can some please tell me what i am doing wrong here.
Upvotes: 0
Views: 1190
Reputation: 32929
You need a mock to stub onto, and from what it looks like, self.myVC
is not a mock.
I'd recommend creating a partial mock for the view controller and stub afterwards.
MyViewController *partialMock = OCMPartialMock(self.myVC)
OCMStub([partialMock myArray]).andReturn(persons);
XCTAssertEqual([partialMock numberOfPersons], 5);
BTW, you don't need the mockArray
usage if you're anyway be stubbing the myArray
getter.
Upvotes: 1