ios – Swift – How can I take a look at with generic?


I wanna unittest with generics.
However I am unable to consider a means.

My code is as beneath, and I need to take a look at the half put together().

class A_ViewModel {
    var useCase: UseCaseProtocol = UseCase()
    var merchandise: A_CellModel = .init()
    
    // I need to unittest put together()
    func put together() {
        // logic....
        
        // and name calcuate
        useCase.calculate(merchandise: merchandise)
    }
}

protocol TestableProtocol {
    var testProperty: Bool { get set }
}

class A_CellModel: TestableProtocol {
    var testProperty: Bool = false
}

protocol UseCaseProtocol {
    func calculate<T: TestableProtocol>(merchandise: T)
}

class UseCase: UseCaseProtocol {
    func calculate<T: TestableProtocol>(merchandise: T) {
        // logic......
    }
}

Nevertheless, since calcaulte(merchandise:) in usecase makes use of generic, it’s needed handy over the clear kind (A_CellModel).

In that case, there’s a dependence between A_ViewModel and A_CellModel, which makes it tough to check the unit.

As a way to take a look at put together(), ought to calcaulte(merchandise:) hand over generic?
Ought to I exploit existential kind?

See also  ios - How you can create a reusable button class with a stack view in UIKit?

Leave a Reply