GithubHelp home page GithubHelp logo

carolanitz / ocadditions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dp-opensource/ocadditions

0.0 0.0 0.0 75 KB

Collection of Classes and Categories for OCUnit with OCMock

License: MIT License

ocadditions's Introduction

OCAdditions

Base Classes

DPTestCase comes without dependencies except SenTestingKit and has methods to let the test case execution wait for a certain interval or a condition to become true. Typical use case is waiting for an animation to finish.

DPBaseTestCase inherits from DPTestCase and has methods to setup and tear down view controllers from Storyboard for testing, and provides a generator for a NSArray of NSManagedObject. Therefore it depends on an AppDelegate with a UIWindow* window property, the DSMockDataGenerator from iOSReusableComponents and the OCMock and OCHamcrest frameworks. It also imports the header files of all the mock categories, so that every subclass uses these categories. You want to customize DPBaseTestCase for your needs, especially change the constants with the names of your storyboard and your momd file and rename the AppDelegate class name.

Test cases should inherit from either DPTestCase or DPBaseTestCase.

Mock Categories

The mockcategories directory contains a set of categories for mocking singletons, inspired by the great article on Mocking singletons with OCMock. There are categories for a few classes from Cocoa Touch, Facebook SDK, Urban Airship, UserVoice and RestKit.

Mocking Shared Instances

Many categories add class methods like createMockObject, createNiceMockObject or createPartialMockObject to initialize and return a mock object for the shared instance of this class. The counterpart is destroyMockObject, which is provided to release the mock object after verification, so the real shared instance can be used. A typical usage example looks like this:

id mockNotificationCenter = [NSNotificationCenter createPartialMockObject];
[[mockNotificationCenter expect] addObserver:[OCMArg isNotNil] selector:NSSelectorFromString(@"sessionStateChanged:") name:FBSessionStateChangedNotification object:[OCMArg isNil]];

// … the tested code is called

STAssertNoThrow([mockNotificationCenter verify], nil);
[NSNotificationCenter destroyMockObject];

Be careful when mocking NSNotificationCenter, especially when performing segues. Some parts of UIKit trigger NSNotifications that other parts need to receive, which is not the case when NSNotificationCenter is currently mocked. This might result in unexpected exceptions.

In order to be able to switch between mock object and real implementation when testing shared instances with categories, the Supersequent Implementation by Matt Galagher is used with the implemented version by Francois Proulx.

Mocking Class Methods

As far as I know, class methods cannot be mocked with OCMock. Therefore, some mock categories override this class method and provide another class method to be called in advance to setup an expectation. This expect method takes a block as argument with a similar signature as the mocked class method, which is executed when the mocked class method is called. That way, you can check inside the block whether the mocked class method was called and check the values of the arguments. Example usage:

__block BOOL verifyTakeOff = NO;
[UAirship expectTakeOff:^(NSDictionary *options) {
    assertThat(options, notNilValue());
    assertThat(options, hasKey(UAirshipTakeOffOptionsAirshipConfigKey));
    verifyTakeOff = YES;
}];

// the code under test
[appdelegate application:mockApplication didFinishLaunchingWithOptions:nil];

assertThatBool(verifyTakeOff, equalToBool(YES));

License

Licensed under the terms of the MIT License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.