lundi 30 mai 2016

stub function is not working in my test case

I am using OCMock 3 to write my unite tests in iOS project.

I have a foo method under School class:

@implementation School
-(NSString *)foo:
{
    // I need to mock this MyService instance in my test
    MyService *service = [[MyService alloc] init];

    // I need to stub the function return for [service getStudent]
    Student *student = [service getStudent];
    if (student.age == 12) {
       //log the age is 12
    } else {
      //log the age is not 12
    }
    ...
}

In my test case, I want to stub the method call [service getStudent] to return a Student instance with age value 12 I defined:

// create a student instance (with age=12) which I want to return by function '-(Student*) getStudent:'
Student *myStudent = [[Student alloc] init];
myStudent.age = 12;

// use mocked service
id mockService = OCMClassMock([MyService class]);
OCMStub([[mockService alloc] init]).andReturn(mockService);

// stub function return on mocked service
OCMStub([mockService getStudent]).andReturn(myStudent);

// execute foo method
id schoolToTest = [OCMockObject partialMockForObject:[[School alloc] init]];
[schoolToTest foo];

When I run my test case, however, the student returned by -(Student*)getStudent: method is not with age 12, why?

Aucun commentaire:

Enregistrer un commentaire