How to use Resolver when writing unit tests

Viewed 1351

I hope all is well and you are enjoying WWDC 2020 online. I have a question. I am using the pod 'Resolver' for dependency injection. It is working great. Until I write my unit tests to test my view models. I get this crash:

    public final func resolve<Service>(_ type: Service.Type = Service.self, name: String? = nil, args: Any? = nil) -> Service {
    if let registration = lookup(type, name: name ?? NONAME),
        let service = registration.scope.resolve(resolver: self, registration: registration, args: args) {
        return service
    }
    print("RESOLVER: '\(Service.self):\(name ?? "")' not resolved. To disambiguate optionals use resover.optional().")
    fatalError()
}

The test:

import XCTest
import Resolver

class QuizRowViewModelModelTests: XCTestCase {

@Published var quizRepository: QuizRepository = Resolver.resolve()

I have debugged the section where it sets QuizRepository and this works fine.

register { TestDataQuizRepository() as QuizRepository }.scope(application)

Do you happen to have any idea as to why this would happen?

Thanks a million

1 Answers

I'd registered the Repository inside of the setUp function.

override func setUp() {
        Resolver.register {
            GetAllGuideShopsRepositoryMock(withSuccess: true)
        }.implements(GetAllGuideShopsRepository.self)
    }

Then, I'd resolved it inside of my test.

sut.getAllGuideShopsRepository = Resolver.resolve(GetAllGuideShopsRepositoryMock.self)

If you want to see more details, please check the Test Class on github.com

Related