Latest 0.4.1
Homepage https://github.com/azubala/OCMockitoSwift
License MIT
Platforms ios 8.0, requires ARC
Dependencies OCMockito, OCHamcrest
Authors

Disclaimer

This library is work in progress, only bare minimum is provided from OCMockito.

About

OCMockito adapter for Swift, that allows to create and operate on mocks which are subclasses for NSObject.

The adapter comes with two files:

  • OCMockitoSwiftAdapter.{h,m} — Objective-C code to provide Swift friendly interface that allows mocking, veryfing and stubbing
  • OCMockito.swift — set of global functions for Swift, e.g. mock() or verify()

Installation

Cocoapods

Simply add to your Podfile:

pod 'OCMockitoSwift', '~> 0.3'

Carthage

Simply add to your Cartfile:

github "azubala/OCMockitoSwift"

Examples

Creating mocks

To create a mock of an NSObject subclass simply call:

let mock = mock(MyObject.self)  // no casting needed

To create a mock of an Objective-C protocol that extends NSObject protocol you execute the following:

let mock = mockProtocol(MyProtocol.self) as? MyProtocol // casting required :(

Verifying behavior

To verify if there were interaction with the mock you use the verify global function:


verify(mock) { #selector(MyObject.doSomethingNoArguments) } // method without arguments

verify(mock) { (#selector(MyObject.doSomething(with:)), arguments: ["Foo"]) } // method with arguments

To verify count of the mock interaction simply use count parameter (by default it’s set to 1). For example to verify that there were no interactions with mock you can simply call:

verify(testMock, count: 0) { #selector(TestClass.doSomethingNoArguments) }

Working with OCHamcrest matchers:


// provide map of matchers for each argument index; `whatever()` is equivalent of `anything()`

verify(testMock) { (#selector(TestClass.doSomething(with:)), matchers: [0: whatever()]) } 

// Capturing arguments

var captor = HCArgumentCaptor()

verify(testMock) { (#selector(TestClass.doSomething(with:)), matchers: [0: captor]) }

var capturedValue = captor.value as! String
expect(capturedValue).to(equal("Bar"))

Stubbing mocks

To stub return value of a particular method from mock you use the given global function:


given(mock) { (#selector(MyObject.returnObjectMethodNoArguments), willReturn: "Fake Value")} // method without arguments

given(mock) { (#selector(MyObject.returnSomething(with:)), arguments: ["Foo"], willReturn: "Fake Value")} // method with arguments

Author

Aleksander Zubala | zubala.com

License

OCMockitoSwift is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "OCMockitoSwift",
    "version": "0.4.1",
    "swift_version": "4.2",
    "summary": "Swift adapter for OCMockito",
    "description": "OCMockito adapter for Swift, that allows to create and operate on mocks which are subclasses of NSObject.",
    "authors": {
        "Aleksander Zubala": "[email protected]"
    },
    "source": {
        "git": "https://github.com/azubala/OCMockitoSwift.git",
        "tag": "0.4.1"
    },
    "homepage": "https://github.com/azubala/OCMockitoSwift",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "Classes/**/*.{h,m,swift}",
    "ios": {
        "frameworks": [
            "Foundation",
            "CoreFoundation"
        ]
    },
    "dependencies": {
        "OCMockito": [
            "~> 5.0.1"
        ],
        "OCHamcrest": [
            "~> 7.0"
        ]
    },
    "requires_arc": true
}

Pin It on Pinterest

Share This