hmlongco / Resolver

Swift Ultralight Dependency Injection / Service Locator framework
MIT License
2.14k stars 188 forks source link

Registration from different Unit Test SetUp looked to during class initialization #93

Closed corban123 closed 3 years ago

corban123 commented 3 years ago

Apologies for the word spaghetti that is the title, however, best I got.

I have ClassA and ClassB

Class A @Inject ServiceA

Class B also @Inject ServiceA

in the Unit Test for ClassA, we initialize a mockServiceA and Resolver.services.register { mockServiceA as ServiceA }

in the Unit Test for ClassB, we don't care about ServiceA, so we do not mock and registerServiceA`.

Upon running all my unit tests, ClassA's unit tests are run first, and on tearDown, we deinitialize the mockServiceA we initialized in SetUp.

When ClassB's unit tests are run, I immediately receive a Unexpectedly found nil while implicitly unwrapping an Optional value, which points to the Resolver.services.register { mockServiceA as ServiceA } from ClassA's unit tests where mockServiceA is what is now nil.

My question is why is this happening, and what is the proper way of handling this?

hmlongco commented 3 years ago

Without a better look at the code I really can't say exactly why. That said, keep in mind that Xcode is running your unit tests by running your application, and it could well be that something in your app wants a ServiceA and now can't find one.

I'd suggest looking at the call stack when the crash occurs and seeing what's going on at that point in time.