Insights on Ruby, Git, jQuery, Cappuccino, WordPress, Debian and OS X. Please subscribe if you find something useful!

Testing Cappuccino Notifications

Posted: January 7th, 2010 | Author: Jerod | Filed under: Cappuccino | Tags: | View Comments

Writing a web app using Cappuccino has a lot of benefits, one of which is a really nice message passing system wherein certain objects can register to observe events and take action when other objects post notifications of those events.

Here is a very basic way to test if your app is posting event notifications as you expect it to. First, create an Observer class inside a test helper file, which will be included into your tests:

// this is TestHelper.j
@import <Foundation/CPObject.j>
 
@implementation Observer : CPObject
{
    CPArray postedNotifications;
}
 
- (id)init
{
    if (self = [super init])
    {
        postedNotifications   = [CPArray array];
    }
    return self;
}
 
- (void)startObserving:(CPString)aNotificationName
{
    [[CPNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(notificationPosted:)
                                                 name:aNotificationName
                                               object:nil];
}
 
- (void)notificationPosted:(id)sender
{
    [postedNotifications addObject:[sender name]];
}
 
- (BOOL)didObserve:(CPString)aNotificationName
{
    return [postedNotifications containsObject:aNotificationName];
}
 
@end

This class can be configured to register for certain notifications via -startObserving and when they are posted, it stores them in an array (postedNotifications). You can then ask it at any time if a notification has been observed using the -didObserve method and it will respond with YES or NO.

So, to use this in your tests, do something like this:

// This is MyTest.j
@import "TestHelper.j"
 
@implementation MyTest : OJTestCase
 
- (void)setUp
{
    observer = [[Observer alloc] init];
}
 
- (void)testMyMethodDidPostNotification
{
    [observer startObserving:@"MyMethodDidFinishExecution"];
    // do stuff that you would expect to get the notification posted
    [self assertTrue:[observer didObserve:@"MyMethodDidFinishExecution"];
}
 
@end

You could get more fancy with this (like allowing object observing and not just name observing), but the concept doesn’t change. Hope this helps anybody thinking about how to test their Capp apps!


View Comments on “Testing Cappuccino Notifications”

  1. 1 Derek Hammer said at 4:19 pm on January 23rd, 2010:

    Good stuff. Our project is using this.

    We've got a couple of things hanging around like this that we use to test. Perhaps there would be some use in a “Test-Extensions” type of repository?

  2. 2 Jerod Santo said at 9:26 pm on January 23rd, 2010:

    I would definitely be interested in such a repository. We could link to it from the Cappuccino wiki under “testing best practices” or something similar.

  3. 3 ugg boots said at 7:21 am on July 5th, 2010:

    Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
    Jordan Pro Classic
    If its common it might be difficult to get, however, be creative and you can usually find something.

  4. 4 moncler jackets said at 6:22 am on July 9th, 2010:

    I’ve been looking for a similar to this post. Not only extensively but also detailly. We can learn a lot from the post. moncler jackets I recommend to you , ugg boots sale you can come communication in here. Let us grow up together.On the other hand ,I know some websites content is very well.you can go and see.Such asugg boots for sale

blog comments powered by Disqus