Latest 0.4
Homepage https://github.com/garyjohnson/okspechelper
License MIT
Platforms ios 7.0, requires ARC
Dependencies Kiwi/XCTest
Frameworks XCTest, Foundation
Authors

Simple helpers for using Objection to inject mocks for Kiwi tests. Tries to stay out of the way of normal Objection and Kiwi syntax and usage.
Currently targets iOS 7 with XCTest.

Installing

To install using Cocoapods, include OKSpecHelper in your Podfile, but only for your test targets:

target :'myTests', :exclusive => true do
  pod 'OKSpecHelper'
end

Usage

Set up Objection how you normally would for your targets.

Create an JSObjectionModule for binding your classes to mocks. Include any dependency that would normally be injected by Objection that you are going to need to mock during test.

@interface TestInjectionModule : JSObjectionModule
@end

@implementation TestInjectionModule

- (void)configure {
      [self bind:[NSNotificationCenter nullMock] toClass:[NSNotificationCenter class]];
      [...]
}

@end

In each spec, instead of using SPEC_BEGIN, use SPEC_BEGIN_WITH_INJECTION, like so. Note the second parameter, which is the name of the mock injection module.

SPEC_BEGIN_WITH_INJECTION(GBGameSpec, TestInjectionModule)

Set up the thing you’re testing and be sure to call injectDependencies on it, since Objection won’t be creating it for you:

SPEC_BEGIN_WITH_INJECTION(GBGameSpec, TestInjectionModule)

        describe(@"GBGame", ^{
            __block GBGame *game;

            beforeEach(^{
                game = [[GBGame alloc] init];
                [game injectDependencies];
            });

            [...]

        });

 SPEC_END

Finally, you can get ahold of your mocks by using getDependency(<class or protocol>). I usually retrieve and store them in the beforeEach, like so:

#import "Kiwi.h"
#import "JSObjection.h"
#import "GBGame.h"
#import "OKSpecHelpers.h"
#import "TestInjectionModule.h"
#import "NSObject+OKInjection.h"

SPEC_BEGIN_WITH_INJECTION(GBGameSpec, TestInjectionModule)

        describe(@"GBGame", ^{
            __block GBGame *game;
            __block NSNotificationCenter *notificationCenterMock;

            beforeEach(^{
                notificationCenterMock = getDependency([NSNotificationCenter class]);

                game = [[GBGame alloc] init];
                [game injectDependencies];
            });

            context(@"when foo", ^{

                it(@"should bar", ^{
                    [game arrange];

                    [[notificationCenterMock should] receive:@selector(assert)];

                    [game act];
                });
            });
        });

 SPEC_END

Note that your injection module (and your mocks) will get rebuilt for each test and not reused, so you should be able to use your mocks with async tests (shouldEventually, etc) without any issue.

Latest podspec

{
    "name": "OKSpecHelper",
    "version": "0.4",
    "summary": "Simple helpers for using Objection framework with Kiwi tests",
    "description": "  Simple helpers for using Objection framework with Kiwi tests.n",
    "homepage": "https://github.com/garyjohnson/okspechelper",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Gary Johnson": "[email protected]"
    },
    "social_media_url": "http://twitter.com/GaryGJohnson",
    "platforms": {
        "ios": "7.0"
    },
    "source": {
        "git": "https://github.com/garyjohnson/okspechelper.git",
        "tag": "0.4"
    },
    "source_files": "*.{h,m}",
    "public_header_files": "*.h",
    "requires_arc": true,
    "frameworks": [
        "XCTest",
        "Foundation"
    ],
    "dependencies": {
        "Kiwi/XCTest": []
    }
}

Pin It on Pinterest

Share This