Re: How do I unit test an object delegate?
Re: How do I unit test an object delegate?
- Subject: Re: How do I unit test an object delegate?
- From: Michael Ash <email@hidden>
- Date: Mon, 9 Feb 2009 00:45:40 -0500
On Sun, Feb 8, 2009 at 10:50 PM, Sean DeNigris <email@hidden> wrote:
> I'm unit testing a cocoa app in xcode by injecting a test bundle into the
> executable, so the unit tests get run at the end of every build.
>
> Example:
>
> @interface AppController : NSObject {
> ...
> IBOutlet NSButton* startButton;
> NSSpeechSynthesizer* speechSynthesizer;
> }
>
> @implementation AppController
> ...
> - (id)init
> {
> [super init];
> speechSynthesizer = [[NSSpeechSynthesizer alloc] initWithVoice:nil];
> [speechSynthesizer setDelegate:self];
> return self;
> }
>
> - (void)speechSynthesizer:(NSSpeechSynthesizer*)sender
> didFinishSpeaking:(BOOL)success
> {
> [startButton setEnabled:TRUE];
> }
> ...
> @end
>
> // Test case
> - (void)testButtons
> {
> STAssertTrue([startButton isEnabled], @"");
>
> // Start speaking
> [controller sayIt:nil];
>
> STAssertFalse([startButton isEnabled], @"");
>
> //Stop speaking
> [controller stopIt:nil];
>
> STAssertTrue([startButton isEnabled], @"");
> }
>
> The final assert fails because didFinishSpeaking never gets called. How do
> I get the delegate to be called?
Asynchronous delegate callbacks like this rely on the runloop to
function. You don't run the runloop anywhere in here so nothing can
happen with it. Stick something like this after your calls to the
controller:
while([startButton isEnabled])
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture];
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden