I have a class for that I want to write unit test. This class has a dependency, which is a generic class, that I want to mock.
This is the class I want to test.
class ClassToTest: SomeProtocol {
private let dependency = DependencyClass<AnotherClass>()
init() {
//Some Code Here
}
}
class DependencyClass<General: AnotherClassProtocol> {
func getData(from general: General) -> AnyPublisher<Data, Error> {
//Some Code Here
}
}
What I tried
class ClassToTest: SomeProtocol {
private let dependency: DependencyClassProtocol
init(_ dependency: DependencyClassProtocol = DependencyClass<AnotherClass>()) {
//Some Code Here
self. dependency = dependency
}
}
class DependencyClass<General: AnotherClassProtocol>: DependencyClassProtocol {
func getData(from general: General) -> AnyPublisher<Data, Error> {
//Some Code Here
}
}
protocol DependencyClassProtocol {
associatedtype General
func getData(from general: General) -> AnyPublisher<Data, Error>
}
This approach gives me error "Protocol can an only be used as a generic constraint because it has Self or associated type requirements".
How can I Mock the DependencyClass to test the exact behaviour of ClassToTest.