You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow tests to initialize non-stubbed dependencies
Add flag `allow-tests-to-init-real-dependencies` that will force all dependency resolver methods to be declared as `internal` rather than `private` or `fileprivate` so that any `@Testable import` of the generated code is able to initialize both the dependency container and access all of the DependencyResolvers. Note that this does **NOT** change any of the test mock generation, this is altering the access level of the **real** dependencies to be able to facilitate customization of test dependencies. It is then possible to mix and match real and mocked dependencies based on your test setup. Previously this was not possible because the MainDependencyContainer initializer was private, along with all of the dependency resolvers. Without setting this flag, there is no impact to code generation so this change is fully backward compatible.
For example, now inside of unit tests for some `CoolSampleApp` , you would be able to do this:
```
@testable import CoolSampleApp
class ExampleTests: XCTestCase {
func testExample() {
let realDependencyContainer = MainDependencyContainer()
let realDependency = realDependencyContainer.specificDependencyResolver().specificDependency
// Test code that actually uses the real dependency
}
}
```
It is still possible to use the `tests` flag to generate stubbed dependencies and use those alongside each other. For example, you can now mix and match real and mocked dependencies for a class under test to be able to do integration or feature level tests. Continuing the `textExample()` from above, you may see something like this:
```
func testExample() {
let realDependencyContainer = MainDependencyContainer()
let realDependencyFoo = realDependencyContainer.dependencyFooResolver().dependencyFoo
let mockedDependencyContainer = StubbedDependencies()
let mockedDependencyBar = StubbedDependencies.dependencyBarDouble
let objectUnderTest = SomeClass(foo: realDependencyFoo, bar: mockedDependencyBar)
// Actual test logic and validation
}
```
Copy file name to clipboardExpand all lines: README.md
+2
Original file line number
Diff line number
Diff line change
@@ -549,6 +549,7 @@ Options:
549
549
--ignored-path - Paths to ignore.
550
550
--cache-path - Where the cache gets stored.
551
551
--recursive-off
552
+
--allow-tests-to-init-real-dependencies - Makes dependency resolvers available to @Testable imports.
552
553
--tests - Activates the test helpers' generation.
553
554
--testable-imports - Modules to imports in the test helpers.
554
555
--swiftlint-disable-all - Disables all swiftlint rules.
@@ -578,6 +579,7 @@ weaver swift --project-path $PROJECT_DIR/$PROJECT_NAME --main-output-path Genera
578
579
-`--platform` - Platform for which the generated code will be compiled (iOS, watchOS, OSX, macOS or tvOS).
579
580
-`--included-imports` - Modules which can be imported in generated files.
580
581
-`--excluded-imports` - Modules which can't be imported in generated files.
582
+
-`--allow_tests_to_init_real_dependencies` - Will declare every DependencyResolver with internal access level so `@Testable import`s can invoke them to initialize real dependencies in tests.
0 commit comments