Madu Madu - 8 months ago 22
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

application and i have some confusion about it.

Lets say i have a class name called
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;


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

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


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];

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