ios - Stubbing Methods does not work with OCMock3.4 -
stubbing methods works nicely 'nsuserdefaults',but doesn't seem work custom method.
my test code follows:
- (void)testexample { id userdefaultmock = ocmclassmock([nsuserdefaults class]); ocmstub([userdefaultmock standarduserdefaults]).andreturn(userdefaultmock); ocmstub([userdefaultmock objectforkey:[ocmarg any]]).andreturn(@"user defaults mock obj"); id managermock = ocmclassmock([viewmanager class]); ocmstub([managermock sharedinstance]).andreturn(managermock); ocmstub([managermock generateobjwithobj:[ocmarg any]]).andreturn(@(11)); viewcontroller *vc = [[viewcontroller alloc]init]; [vc publicmethod]; }
code in viewcontroller:
- (void)publicmethod { [[nsuserdefaults standarduserdefaults]setobject:@"mockobj" forkey:@"mockobjkey"]; id obj = [[nsuserdefaults standarduserdefaults]objectforkey:@"mockobjkey"]; nslog(@"mockobjkey:%@",obj); nsnumber *num = @(0); nsnumber *i = [[viewmanager sharedinstance] generateindexwithnumber:num]; nslog(@"i expected 1, actual :%ld",i.integervalue); }
code in viewmanager:
static viewmanager *sharedviewmanager = nil; + (id)sharedinstance { if (nil == sharedviewmanager) { sharedviewmanager = [[viewmanager alloc]init]; } return sharedviewmanager; } - (nsnumber *)generateindexwithnumber:(nsnumber *)index { nslog(@"generateindexwithnumber:%ld",index.integervalue); return @(index.integervalue + 1); }
and console output follows:
2017-07-03 10:12:25.812 oc_unit[2214:64454] mockobjkey:user defaults mock obj 2017-07-03 10:12:25.812 oc_unit[2214:64454] generateindexwithnumber:0 2017-07-03 10:12:25.812 oc_unit[2214:64454] expected 1, actual :1
the last line of output should 'i expected 1, actual :11' if shubbing methods works. i'm wondering,why not output expected。
it looks stubbing generateobjwithobj:
code calling generateindexwithnumber:
.
Comments
Post a Comment