Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to implement different behavior between app and test targets? #107

Open
drmarkpowell opened this issue Sep 16, 2018 · 0 comments
Open

Comments

@drmarkpowell
Copy link

Hey, this is a really cool DI utility and I am using it on pretty much all my projects now.
One thing the DI really helps with is to allow test fixture to inject mock databases, network, etc. for apps to test internal functionality without external dependencies.

When I have an iOS app that's configured with a SwinjectStoryboard (swift) class like this:

import Swinject
import SwinjectStoryboard

extension SwinjectStoryboard {
    @objc class func setup() {

...and I want to register my data provider like this example

defaultContainer.register(DataModel.self) { _ in RealmServerModel() }
            .inObjectScope(.container) //make it a singleton

but I want to write unit tests in my tests target that instantiate ViewControllers using the storyboard

        let container = Container()
        container.register(DataModel.self) { _ in TestRealmDataModel.self() }
        let testDataModel = container.resolve(DataModel.self) as! TestRealmDataModel
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        describe("ViewController") {
            var vc: ViewController!
            beforeEach {
                vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController

...what I find is that the SwinjectStoryboard runs first, before my test fixture gets in there to create a container to register the model class for testing.

My workaround has been to avoid running the code in SwinjectStoryboard if it's running tests, like so:

        if ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] == nil {
            // Code only executes when tests are not running

Is there a more elegant way that this could be done?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant