ocunit - How to assert a UILabel.text property is equal to an instance of NSString in objective-c -


i'm new objective-c , i'm finding don't know how correctly assert text property on given label equal raw string value. i'm not sure if need cast label nsstring or if need modify assert statement directly.

@interface moretest : sentestcase {   magiczztestingviewcontroller* controller; }  - (void) testobj;  @end  @implementation moretest  - (void) setup {   controller = [[magiczztestingviewcontroller alloc] init]; }  - (void) teardown {   [controller release]; }  - (void) testobj {   controller.domagic;    stassertequals(@"hehe", controller.label.text, @"should hehe, %d instead", valtxt); }  @end 

the implementation of domagic method below

@interface magiczztestingviewcontroller : uiviewcontroller {   iboutlet uilabel *label; }  @property (nonatomic, retain) uilabel *label; - (void) domagic;  @end  @implementation magiczztestingviewcontroller @synthesize label;  - (void) domagic  {   label.text = @"hehe"; }  - (void)dealloc {   [label release];   [super dealloc]; }  @end 

the build fine when modify assert compare raw nsstring when try capture text value (assuming it's of type nsstring) fails. appreciated!

you need load nib of view controller. otherwise there won't objects label outlet hooked to.

one way add ivar view controller's view test case:

@interface moretest : sentestcase {     magiczztestingviewcontroller *controller;     uiview *view; } @end  @implementation moretest  - (void)setup {     [super setup];      controller = [[magiczztestingviewcontroller alloc] init];     view = controller.view; // owned controller }  - (void)teardown {     view = nil; // owned controller     [controller release];      [super teardown]; }  - (void)testviewexists {     stassertnotnil(view,         @"the view controller should have associated view."); }  - (void)testobj {     [controller domagic];      stassertequalobjects(@"hehe", controller.label.text,         @"the label should contain appropriate text after magic."); }  @end 

note need invoke super's -setup , -teardown methods appropriately within yours.

finally, do not use dot syntax method invocation, not generic replacement bracket syntax in message expressions. use dot syntax only getting , setting object state.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -