call resetMockSix() at the beginning of each test (typically in a beforeEach block)
instantiate and inject as usual:
let foobar = MockFoobar()
let sut = MyClass(foobar: foobar)
stub methods by referring to their method ID:
// return value override
foobar.stub(.doThis, andReturn: [42])
// replace implementation with closure
foobar.stub(.doThis) { (args: [Any?]) in
let num = args[1]! as! Int
return [num]
}
foobar.stub(.doThat) { _ in
if arc4random() % 2 == 1 { throw FoobarError.unknown }
return 3.14
}
// invocation count aware stubbing
foobar.stub(.doThis, andReturn: [42], times: 1, afterwardsReturn: [43])
CAVEAT: the return value type must exactly match that of the function, e.g. to return a conforming SomeClass instance from a function with SomeClassProtocol return type, use explicit casting:
foobar.stub(.whatever, andReturn: SomeClass() as SomeClassProtocol)
remove stubs to restore the behavior defined in the mock implementation:
foobar.unstub(.doThat)
access raw invocation logs (if you really need to; otherwise you are better off with the Nimble matchers):
// the mock has not been accessed
foobar.invocations.isEmpty
// doThis(_:_:) has been called twice
foobar.invocations
.filter { $0.methodID == MockFoobar.Methods.doThis.rawValue }
.count == 2
// doThis(_:_:) has been called with ("42", 42)
!foobar.invocations
.filter {
$0.methodID == MockFoobar.Methods.doThis.rawValue &&
$0.args[0]! as! String == "42" &&
$0.args[1]! as! Int == 42
}
.isEmpty
Other stuff
I also wrote two blogposts about MockSix which may help you get started:
Invocation logs are showing unrelated calls => try calling resetMockSix() in the setup phase of each test case
Test crashes with cast error => make sure the types of the returned values match the return type of the stubbed function; use explicit casting where required
MockSix
MockSix is a microframework to make object mocking in Swift somewhat easier. MockSix is built upon Daniel Burbank’s MockFive.
If you are using Quick+Nimble, make sure you check out the Nimble matcher extensions at NimbleMockSix as well.
Elevator pitch
MockSix simplifies manual object mocking by taking over some of the boilerplate and offering an API that is hard to use incorrectly.
Sample code:
Requirements
To build: Swift 4.2
To use: macOS 10.10+, iOS 8.4+, tvOS 9.2+, Linux
Installation
Via Cocoapods: add the following line to your Podfile:
Via Carthage: add the following line to your Cartfile (or Cartfile.private):
Via the Swift Package Manager: add it to the dependencies in your Package.swift:
Or just add
MockSix.swift
andMockSixInternal.swift
to your test target.Usage
Creating the mock implementation
Conform to Mock besides the actual protocol you are creating the mock for:
Declare an enum for the methods (“method ID”) you want to make available in the mock and set it for the
MockMethod
typealias:The enum must have a
RawValue
ofInt
.Implement the methods by calling through
registerInvocation
orregisterThrowingInvocation
:Define any properties mandated by the protocol:
Using the mock
call
resetMockSix()
at the beginning of each test (typically in abeforeEach
block)instantiate and inject as usual:
stub methods by referring to their method ID:
CAVEAT: the return value type must exactly match that of the function, e.g. to return a conforming
SomeClass
instance from a function withSomeClassProtocol
return type, use explicit casting:remove stubs to restore the behavior defined in the mock implementation:
access raw invocation logs (if you really need to; otherwise you are better off with the Nimble matchers):
Other stuff
I also wrote two blogposts about MockSix which may help you get started:
Troubleshooting
resetMockSix()
in the setup phase of each test caseLicense
MockSix is released under the MIT license.