ios - OCMock setup mock class, its properties and get basic understanding -
ios - OCMock setup mock class, its properties and get basic understanding -
i new tdd thing , writing test cases in general eager , understand. though @ moment have hard time head around syntax , way of thinking stubing , mocking.
but aware need basic hang of "aaah" moment.
atm not there. basic know , case:
i have illustration auto class object coredata. init ofc
[mycar alloc] initwithentity:<(nsentitydescription *)> insertintomanagedobjectcontext:<(nsmanagedobjectcontext *)>
i want test:
- (cllocationcoordinate2d) locationcoordinatesatindex: (nsinteger) index { cllocationcoordinate2d returnvalue = cllocationcoordinate2dmake(0.0f, 0.0f); /* sanity check */ if (index >= 0 && index < self.carslist.count) { mycar* coolcar = self.carslist[index]; //array mycar objects returnvalue = cllocationcoordinate2dmake(self.coolcar.latitude.doublevalue, self.coolcar.longitude.doublevalue); } homecoming returnvalue; }
this method exist in carviewcontroller
so thinking - ok gonna mock mycar
carviewcontroller
, , add together fixed values self.coolcar.latitude
, self.coolcar.longitude
doing:
@interface carviewcontroller @property (nonatomic, strong) mycar *coolcar; @property (nonatomic, strong) nsmutablearray *carslist; //because existing private in .m @end @interface mycar (test) @property (nonatomic, strong) nsnumber *latitude; //because existing private in .m @property (nonatomic, strong) nsnumber *longitude; //because existing private in .m @end @interface wtstestcarlocation : xctestcase @property (nonatomic, strong) carviewcontroller *carvc; @end -(void)testcarloc{ self.carvc = ocmclassmock([wtsarviewcontroller class]);` self.coolcar = [ocmockobject mockforclass:[mycar class]]` self.coolcar.latitude = @7.05444; self.coolcar.longitude = @125.601087; [self.carslists addobject:self.coolcar]; ocmstub([self.carvc locationcoordinatesatindex:0]); /* atm log */ nslog(@"the location2dlat %f",[self.carvc locationcoordinatesatindex:0].latitude);` }
i errors compiler says "doesnotrecognizeselector:" failing on line
self.coolcar.latitude = @7.05444;
so q: need partial mockup insted? , how nsentitydescription
, nsmanagedobjectcontext
any help, pointers , on appreciated
is possible latitude , longitude read-only properties in initial definition , don't have setter defined?
you have right idea. should not stub method trying test, , don't need test have own fellow member variables, can create local variables in test method itself.
i'd suggest approach, notated in ocmock 3.x syntax:
-(void)testcarloc { // create test info double testlatitude = 7.05444; double testlongitude = 125.601087; // utilize partial mock auto object "set" private property id *carpartial = ocmpartialmock([mycar new]); ocmstub([carpartial latitude]).andreturn(@(testlatitude); ocmstub([carpartial longitude).andreturn(@(testlongitude)); // utilize partial mock object under test "set" private property id *carvcpartial = ocmpartialmock([carviewcontroller new]); ocmstub([carvcpartial carslist]).andreturn(@[carpartial]); // perform action want test cllocationcoordinate2d result = [carvcpartial locationcoordinatesatindex:0]; // verify results xctassertequals(result.latitude, testlatitude); xctassertequals(result.longitude, testlongitude); }
ios objective-c testing tdd ocmock
Comments
Post a Comment