nalexn / ViewInspector

Runtime introspection and unit testing of SwiftUI views
MIT License
2.09k stars 145 forks source link

Injecting Multiple EnvironmentObjects to a View #310

Closed kevinwywang closed 3 weeks ago

kevinwywang commented 3 weeks ago

Hi, I have two EnvironmentObjects I want to inject into a view.

In testing with ViewInspector, I tried injecting both environment objects with the following: ViewHosting.host(view: sut.environmentObject(FirstEnvironmentObject) ViewHosting.host(view: sut.environmentObject(SecondEnvironmentObject)

However, I get the error of Possible blockers being missing EnvironmentObjects.

If I comment out the code for the SecondEnvinronmentObject, and only inject the FirstEnvironmentObject the tests will pass and no errors are thrown.

Is there a way for me to inject both of my environment objects?

nalexn commented 3 weeks ago

Did you try

ViewHosting.host(view: sut.environmentObject(FirstEnvironmentObject)
                          .environmentObject(SecondEnvironmentObject))

?

kevinwywang commented 3 weeks ago

Ah of course... I should have thought of that, makes a lot of sense.

Just tried it, and it works, thanks for the quick response Alexey!