Is this the correct way to write unit test for Interactor in VIPER, swift, iOS?

Viewed 913

I have a small sorting function in the Interactor which takes a set of Strings and sort it and notify to the Presenter. That function works as expected.

Now I'm just trying cover that function with unit test. so I created a Unit Test Class for my Interactor and Mocked the Interactor Output and did the test like below. am I doing it right?

screen shot of unit-test source code

shouldn't we end the test inside the testFunction, in this case inside the testSortFunction()

This triggers notifySortedNames() in the Mock Interactor Output Class and I tested the use case inside that. I guess this is the wrong way.

How can I achieve the testing of this scenario inside the testFunction()? I'd appreciate your help with this.

Interactor

class DetailInteractor: DetailInteractorInput {
    var output: DetailInteractorOutput?
    
    init(output: DetailInteractorOutput) {
        self.output = output
    }
    
    func sortNames(names: [String]) {
        
        let sorteNames = names.sorted(by: { $0 < $1 })
        output?.notifySortedNames(names: sorteNames)
    }
}

Interactor Test Class

import XCTest
@testable import UnitTesst

class DetailInteractorTest: XCTestCase {
    
    var interactor: DetailInteractor!
    var output: DetailInteractorOutput!

    override func setUpWithError() throws {
       
        output = DetailInteractorOutputMock()
        interactor = DetailInteractor(output: output)
    }

    override func tearDownWithError() throws {
        
    }

    func testSortFunction() throws {
        
        let names = ["Japan", "France", "Australia"]
        interactor.sortNames(names: names)
    }

    func testPerformanceExample() throws {
        
        self.measure {
            
        }
    }
}

// Mock Test Classes
class DetailInteractorOutputMock: DetailInteractorOutput {
    
    func notifySortedNames(names: [String]) {
        
        let expected = ["Australia", "France", "Japan"]
        XCTAssertEqual(expected[0], names[0])
        XCTAssertEqual(expected[1], names[1])
        XCTAssertEqual(expected[2], names[2])
    }
}

Thanks!!

0 Answers
Related