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

Help using inspection #187

Open
edoardop13 opened this issue Aug 22, 2022 · 4 comments
Open

Help using inspection #187

edoardop13 opened this issue Aug 22, 2022 · 4 comments
Labels
help wanted Extra attention is needed

Comments

@edoardop13
Copy link

edoardop13 commented Aug 22, 2022

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!

@nalexn
Copy link
Owner

nalexn commented Sep 10, 2022

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 nalexn added the help wanted Extra attention is needed label Sep 10, 2022
@edoardop13
Copy link
Author

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.

@nalexn
Copy link
Owner

nalexn commented Sep 19, 2022

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.

@edoardop13
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants