ios - Using OCMock with Unknown Number of Method Calls -


i'm using ocmock in junction ghunit try , recreate graham lee's "browseoverflow" project test-driven ios development.

my understanding mock object isn't current class testing. working question class relies on answer class functionality.

questsion.h

@class answer;  @interface question : nsobject {     nsmutableset *answerset; }  @property (retain) nsdate *date; @property nsinteger score; @property (copy) nsstring *title; @property (readonly) nsarray *answers;  - (void)addanswer:(answer *)answer;  @end 

question.m

#import "question.h" #import "answer.h"  @implementation question  @synthesize date; @synthesize score; @synthesize title;  - (id)init {     if ((self = [super init])) {         answerset = [[nsmutableset alloc] init];     }     return self; }  - (void)addanswer:(answer *)answer {     [answerset addobject:answer]; }  - (nsarray *)answers {     return [[answerset allobjects] sortedarrayusingselector:@selector(compare:)]; }  @end 

answer.h

#import <foundation/foundation.h>  @class person;  @interface answer : nsobject  @property (readwrite) nsstring *text; @property (readwrite) person *person; @property (readwrite) nsinteger score; @property (readwrite, getter = isaccepted) bool accepted;  - (nscomparisonresult)compare:(answer *)otheranswer;  @end 

answer.m

#import "answer.h"  @implementation answer  @synthesize text; @synthesize person; @synthesize score; @synthesize accepted;  - (nscomparisonresult)compare:(answer *)otheranswer {     if (accepted && !(otheranswer.accepted)) {         return nsorderedascending;     } else if (!accepted && otheranswer.accepted) {         return nsordereddescending;     }     if (score > otheranswer.score) {         return nsorderedascending;     } else if (score < otheranswer.score) {         return nsordereddescending;     }     return nsorderedsame; }  @end 

i tried using ocmock sub in instances of answer when testing worked 10% of time.

- (void)testhighscoreanswerbeforelow {     lowscore = [ocmockobject mockforclass:[answer class]];     [[[lowscore stub] andreturn:ocmock_value((nsinteger) {4})] score];     [question addanswer:lowscore];      highscore = [ocmockobject mockforclass:[answer class]];     [[[highscore stub] andreturn:ocmock_value((nsinteger) {-4})] score];     [question addanswer:highscore];      [[lowscore expect] compare:[ocmarg any]];      nsarray *answers = question.answers;     nsinteger highindex = [answers indexofobject:highscore];     nsinteger lowindex = [answers indexofobject:lowscore];     ghasserttrue(highindex < lowindex, @"high-scoring index comes first");      [highscore verify];     [lowscore verify]; } 

i think problem conflict caused how nsset stores objects in memory (somewhat randomly) , fact ocmock checks make sure no methods called. settled taking ocmock out of particular test seems bad test.

- (void)testhighscoreanswerbeforelow {     lowscore = [[answer alloc] init];     lowscore.score = -4;     [question addanswer:lowscore];      highscore = [[answer alloc] init];     highscore.score = 4;     [question addanswer:highscore];      nsarray *answers = question.answers;     nsinteger highindex = [answers indexofobject:highscore];     nsinteger lowindex = [answers indexofobject:lowscore];     ghasserttrue(highindex < lowindex, @"high-scoring index comes first"); } 

is possible ocmock play nice sorting algorithms when don't know how many comparisons need make? if not, disadvantage of using ocmock or poorly structured code?

the problem you're setting expectation on compare. if don't care how many times it's called, shouldn't tell mock expect called once.

you can use nicemock if don't care unexpected methods called on mock objects.

you can use stub return value , not care how many times it's called.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -