Madu Madu - 3 months ago 8
Objective-C Question

Creating dummy object with values with OCMOCK

It was my first time i was trying to use OCMOCK framework for testing my

iOS
application and i have some confusion about it.

Lets say i have a class name called
School
and i want to test it. This class contains students. Lets say i want to start the school by adding a student, so i do some thing like this
School *school = [School alloc] initWithOneStudent:student];


Now Student Class is like this.

@interface Student : NSObject
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;

@end


Now how i can get a student object in my test case which has some values for
firstName
and
lastName
when i mock it to pass it to
initWithOneStudent


I just made up this example, so please excuse if you don't like this example.

Answer

For your test case you can make it like below

- (void)testCase {
    id mockStudent = OCMClassMock([Student class]);
    OCMStub([mockStudent firstName]).andReturn(@"firstName");
    OCMStub([mockStudent lastName]).andReturn(@"lastName");

    School *school = [School alloc] initWithOneStudent:mockStudent];

    XCTAssertNotNil(school);
    XCTAssertEqual(school.student.firstName, @"firstName");
    XCTAssertEqual(school.student.lastName, @"lastName");
}