NimbleMockSix 0.1.4

NimbleMockSix 0.1.4

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jan 2018
SwiftSwift Version 3.1
SPMSupports SPM

Maintained by Tamas Lustyik.



 
Depends on:
MockSix= 0.1.7
Nimble~> 7.0
 

Nimble matchers for MockSix.

Teaser

With NimbleMockSix, you can easily make expectations on method invocations on MockSix mock objects. Suppose you have the following mock already in place (for details, see the MockSix documentation):

// original interface to mock
protocol MyClassProtocol {
    func myFunc(string: String, number: Double) -> [Int]
}

// original implementation
class MyClass: MyClassProtocol {
    func myFunc(string: String, number: Double) -> [Int] {
        // ... whatever ...
        return [1, 2, 3]
    }
}

// mock implementation
class MockMyClass: MyClassProtocol, Mock {
    enum Methods: Int {
        case myFunc
    }    
    typealias MockMethod = Methods
    
    func myFunc(string: String, number: Double) -> [Int] {
        return registerInvocation(for: .myFunc, 
                                  args: string, number 
                                  andReturn: [])
    }
}

To test for the invocation count of a method:

// given
let myMock = MockMyClass()
myMock.stub(.myFunc, andReturn: [42])

// when
myMock.myFunc(string: "aaa", number: 3.14)
myMock.myFunc(string: "bbb", number: 6.28)
    
// then
expect(myMock).to(receive(.myFunc, times: 2))   // --> passes

To test the arguments of an invocation:

// given
let myMock = MockMyClass()
myMock.stub(.myFunc, andReturn: [42])

// when
myMock.myFunc(string: "aaa", number: 3.14)

expect(myMock).to(receive(.myFunc, with: [
    theValue("aaa"), 
    any()
]))    // --> passes

expect(myMock).to(receive(.myFunc, with: [
    any(of: ["bbb", "ccc"]), 
    any { x in x >= 3.0 && x < 4.0 }
]))    // --> fails

But there is more!

Currently implemented matchers:

  • just invocation count constraints: receive(_:times:), receive(_:atLeastTimes:), receive(_:atMostTimes:)
  • invocation count AND argument constraints: receive(_:times:with:), receive(_:atLeastTimes:with:), receive(_:atMostTimes:with:):

Currently implemented argument verifiers:

  • theValue(_:): argument matches the given value
  • nilValue(): argument is nil
  • any(): argument matches anything (always passes)
  • any(of:): argument matches any of the values in the array
  • any(passing:): argument makes the predicate true

Requirements

To build: Swift 3.1
To use: macOS 10.10+, iOS 8.4+, tvOS 9.2+, Linux

Installation

Via Cocoapods: add the following line to your Podfile:

pod 'NimbleMockSix'

Via the Swift Package Manager: add it to the dependencies in your Package.swift:

let package = Package(
    name: "MyAwesomeApp",
    dependencies: [
        .Package(url: "https://github.com/lvsti/NimbleMockSix", majorVersion: 0),
        .Package(url: "https://github.com/Quick/Quick", majorVersion: 1),
        // ... other dependencies ...
    ]
)

License

NimbleMockSix is released under the MIT license.