XCTest expects method call with fast

How to write a test waiting for a method call using fast and XCTest?

I could use OCMock, but they do not officially support fast, so there are not many options.

+7
ios ios8 swift xctest ocmock
source share
1 answer

As you said, OCMock does not support Swift (or OCMockito), so now the only way I can see is to create a manual layout. In the fast, this is a little less painful, since you can create inner classes inside the method, but still not as convenient as a mocking structure.

Here you have an example. The code itself is explicit, the only thing I needed to do (see EDITING 1 below) to make it work was to declare the classes and methods that I want to use from the test as public (it seems that the test classes do not belong the same application code module - will try to find a solution for this).

EDIT 1 2016/4/27: Declaring classes that you want to check as public is no longer required, as you can use the "@testable import ModuleName" function.

Test:

import XCTest import SwiftMockingPoC class MyClassTests: XCTestCase { func test__myMethod() { // prepare class MyServiceMock : MyService { var doSomethingWasCalled = false override func doSomething(){ doSomethingWasCalled = true } } let myServiceMock = MyServiceMock() let sut = MyClass(myService: myServiceMock) // test sut.myMethod() // verify XCTAssertTrue(myServiceMock.doSomethingWasCalled) } } 

Myclass.swift

 public class MyClass { let myService: MyService public init(myService: MyService) { self.myService = myService } public func myMethod() { myService.doSomething() } } 

MyService.swift

 public class MyService { public init() { } public func doSomething() { } } 
+4
source share

All Articles