I am looking for a way to test if a service has already been resolved in the .Container scope. My application fires up a few "services" at launch and I wanted to make sure those services have been resolved without triggering them to be resolved.
Asked
Active
Viewed 751 times
1 Answers
0
You can add logging in the factory closure to check whether the type is already resolved by the Swinject container.
container.register(AnimalType.self) { _ in
// You can log here.
print("AnimalType is being resolved to Cat.")
return Cat()
}
.inObjectScope(.Container)
EDIT
Or you can use a flag to check in your unit test.
var initialized = false
container.register(AnimalType.self) { _ in
// You can check this parameter later in your unit test.
initialized = true
return Cat()
}
.inObjectScope(.Container)
XCTAssertFalse(initialized)
container.resolve(AnimalType.self)
XCTAssertTrue(initialized)

Yoichi Tagaya
- 4,547
- 2
- 27
- 38
-
More so looking to do it programmatically in functional or unit tests. I would like to test if a service has been init'd. – MPiccinato Mar 25 '16 at 18:51
-
The flag usage might not be what you want... I just noticed your requirement now. – Yoichi Tagaya Mar 26 '16 at 01:45
-
I could use something like this for now. I was going through the source trying to figure out how to run this sort of test but Xcode and @testable were not playing too nice. – MPiccinato Mar 29 '16 at 12:50