ViewInspector icon indicating copy to clipboard operation
ViewInspector copied to clipboard

Help using inspection

Open edoardop13 opened this issue 3 years ago • 4 comments

Hi, I have a view with environmentObject, inside this view there is another view with another environmentObject. Ho can I inspect this view? This is the solution that I thought, but the compiler don't enter inside the second inspection.

       let expectation = view.inspection.inspect { view in
           code
            _ = view2.inspection.inspect { view in
                code
            }
            ViewHosting.host(view:
                                view2
                .environmentObject(innerViewModelMock as InnerViewModel )
            )
        }
        ViewHosting.host(view:
                            view
            .environmentObject(viewModelMock as ViewModel)
        )

}

Thanks!

edoardop13 avatar Aug 22 '22 10:08 edoardop13

Hey, you cannot host views asynchronously and nest inspect calls, that's why the second closure doesn't work. I'd recommend you breaking down this test into two, where there is a single view being hosted and inspected per test. From your example, it's not clear why you want to inspect both view from a single test. You should provide more context for further assistance

nalexn avatar Sep 10 '22 17:09 nalexn

Hello, Thanks for your advice, I have a view that contains two other views, only one of which is shown. The view to be shown is chosen by a condition. In the first of these two views there is a button that activates the control for this condition. I want to test that from the parent view once the button is pressed and the condition is met the view is changed correctly from the first view to the second.

edoardop13 avatar Sep 19 '22 13:09 edoardop13

Ok, depending on the state management between the parent and child views there could be two variations, where you either will or will not have access to updated child view state from inside the parent's inspection call.

If your child views require async inspection and personal view hosting (for @State, @StateObject or @EnvironmentObject) you won't have access to correct view state from the context of the parent. In this case you still can verify in the second view is present or not by triggering the tap and inspecting the view's presence right after, without nested inspections or hostings.

If your child views do use @Binding or @ObservedObject you should be able to inspect their internals after triggering the tap, again from the context of the parent's inspection call.

Either way: nested inspections or multiple view hostings within a single test are not guaranteed to work.

nalexn avatar Sep 19 '22 14:09 nalexn

Hi, thanks again for the advice, the state managment between the two views is the first that you describe. The problem is that the button is in the child view and when i try to tap it the test fails because i miss the @EnvironmentObject , how can i avoid this? I can test the view changing by directly calling the method triggered by the button.

edoardop13 avatar Sep 20 '22 12:09 edoardop13