XCTestCase to check if a method is called within a Struct XCTestCase to check if a method is called within a Struct ios ios

XCTestCase to check if a method is called within a Struct


Use a protocol. Make your class/struct and your test mock conform to the protocol. Inject the dependency and assert that the expected method gets called in your mock.

Edit: Example

protocol DataApiProtocol {    func getApplicationData(block: [AnyObject] -> Void)}// Production codestruct DataApi: DataApiProtocol {    func getApplicationData(block: [AnyObject] -> Void) {        // do stuff    }    // more properties and methods}// Mock codestruct MockDataApi: DataApiProtocol {    var getApplicationDataGotCalled = false    func getApplicationData(block: [AnyObject] -> Void) {        getApplicationDataGotCalled = true    }}// Test codefunc testGetData_CallsGetApplicationData() {    let sut = MyAwesomeClass()    let mockDataApi = MockDataApi()            sut.getData(mockDataApi)    XCTAssertTrue(mockDataApi.getApplicationDataGotCalled)}

I hope this helps.