Pankaj Teckchandani
Pankaj Teckchandani

Reputation: 745

Test case to check if Notification Received displays an alert view

I m writing a test case for the following condition

I Have a class (sub class of NSObject) which makes a service call to add a new customer. when it gets a successful response it sends a notification to the view co

Now i want to test that the view controller successfully receives notification and displays the correct alert view.

here is my test case code

-(void) testAlertViewDisplayOnSuccessfullAdditionOfCustomer{

    id mockAlertView = [OCMockObject mockForClass:[UIAlertView class]];

    (void)[[[mockAlertView expect] andReturn:mockAlertView]
       initWithTitle:@"myAppName"
       message:@"Submitted Successfully"
       delegate:nil
       cancelButtonTitle:@"OK"
       otherButtonTitles:OCMOCK_ANY, nil];



    [[mockAlertView expect] show];

    [[NSNotificationCenter defaultCenter]postNotificationName:@"notifcationName" 
    object:nil userInfo:@{@"mykey" : @"Submitted"}];

    [mockAlertView verify];
}

But this code isn't working. It crashes at post notification call. Where m i going wrong ?

Upvotes: 0

Views: 309

Answers (2)

Mithun kumar
Mithun kumar

Reputation: 662

add

[[[mockAlertView stub] andReturn:mockAlertView] alloc];

after creating mock object for your alert view.

Upvotes: 1

Ethan Fischer
Ethan Fischer

Reputation: 1489

Is testAlertViewDisplayOnSuccessfullAdditionOfCustomer getting called? You can check by using a breakpoint.

Perhaps you can use didReceiveLocalNotification in your AppDelegate.m and add your code to the delegate instead?

- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html?hl=ar#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveLocalNotification:

Upvotes: 0

Related Questions