A Swift mocking framework inspired by Sinon.js to easily spy on your test doubles or turn them into stubs.
Testable is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Testable'
Let's say you have a test double and you'd like to know whether it's methods are beeing called by the code you're currently testing.
class SomeClass {
func someMethod(foo: Int, bar: Bool) {
//
}
}
By adopting the Spyable protocol and calling recordCall(withArgs:) from the method's implementation, you'll be able to record how many times this method was called and with what arguments.
import Testable
class SomeClass: Spyable {
func someMethod(foo: Int, bar: Bool) {
recordCall(withArgs: foo, bar)
}
}
In your tests you're now able to get a reference to the spy that keeps track of the calls to this method.
func testImplementationCallsSomeMethod() {
let someObject = SomeClass()
let someOtherObject = SomeOtherClass(object: someObject)
someOtherObject.doSomething()
let spy = someObject.spy(forMethod: "someMethod(foo:bar:)")
XCTAssertTrue(spy.called)
XCTAssertTrue(spy.calledOnce)
XCTAssertEqual(spy.firstCall.args[0] as? Int, 42)
XCTAssertEqual(spy.firstCall.args[1] as? Bool, true)
}
You could do the same for your test double's properties.
import Testable
class SomeClass: Spyable {
var someProperty: Int {
get { recordGetProperty() }
set { recordSetProperty(withValue: newValue) }
}
}
In your tests you can get a reference to the property spies.
func testImplementationGetsAndSetsProperty() {
let someObject = SomeClass()
let someOtherObject = SomeOtherClass(object: someObject)
someOtherObject.doSomethingElse()
let getPropertySpy = counter.spy(forGetProperty: "someProperty")
XCTAssertTrue(getPropertySpy.called)
XCTAssertTrue(getPropertySpy.calledOnce)
let setPropertySpy = counter.spy(forSetProperty: "someProperty")
XCTAssertTrue(setPropertySpy.called)
XCTAssertTrue(setPropertySpy.calledOnce)
XCTAssertEqual(setPropertySpy.firstCall.args[0] as? Int, 0)
}
Coming soon…
Pim Nijman, @pnijman
Testable is available under the MIT license. See the LICENSE file for more info.