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

Can View Inspector provide more descriptive errors when throwing? #155

Open
nickg-ifit opened this issue Jan 5, 2022 · 3 comments
Open
Labels
feature request New feature or request

Comments

@nickg-ifit
Copy link

Using XCTAssertNoThrow(try sut.inspect().find(text: "Hello World!")) is a nice convenient way to assert whether or not my view contains a particular child, however in this case that this fails, the thrown error offers a fairly generic message: caught error: "Search did not find a match"

Would it be possible to update the apis such that thrown errors provide some details that make it easier to track down what it was actually searching for? If I write a unit test where I look for multiple children of the same parent view, it one of them fails, it can be difficult to track down which one actually failed (especially when running on a build machine, rather than locally).

@nalexn
Copy link
Owner

nalexn commented Jan 5, 2022

Hey @nickg-ifit I'm happy to make the message more descriptive for this case, though could you provide an example of the hierarchy and the desired message thrown?
It can potentially provide a text representation of the view graph it traversed, if that can be of any help

@nickg-ifit
Copy link
Author

Hmm. A text representation of the view graph would probably be helpful, just for the sake of understanding actual vs expected. What's most important is that from reading the message, I can easily tie that back to the actual line of code for the thing I was searching for.

One of the errors I've seen thrown is a bit more helpful:

XCTAssertNoThrow failed: threw error "view(MyView.self).list(2) found Button<SwiftUI.Text> instead of List"

From this message, I can tell that the view I was even calling list on isn't even a list, but just from the first part of this message, view(MyView.self).list(2), that's easy to tie back to the unit test code line that failed and know what assertion I was checking here.

So in the case of try sut.inspect().find(text: "Hello World!"), I would expect to see a message that let me know that a search for a Text view with string "Hello World!" failed to find a match. Basically, whatever criteria is search for should be captured in the thrown error when the search fails to find a match.

For the text representation of the view graph, it might be useful if that were an extra property of the thrown error but not necessarily part of the default description that would print (that could make the logs annoyingly noisy if you had several successive failures, I think).

@nalexn nalexn added the feature request New feature or request label Sep 10, 2022
@nalexn
Copy link
Owner

nalexn commented Jul 13, 2023

Hey, I just realized the file and line of where the exception happened is actually logged by the test suit itself. Isn't it what you were looking for?

Test Suite 'ViewSearchTests' started at 2023-07-14 00:12:22.034
Test Case '-[ViewInspectorTests.ViewSearchTests testFindText]' started.
.../ViewInspectorTests/ViewSearchTests.swift:127: error: -[ViewInspectorTests.ViewSearchTests testFindText] : failed: caught error: "Search did not find a match"
Test Case '-[ViewInspectorTests.ViewSearchTests testFindText]' failed (0.244 seconds).

It is certainly possible to capture the caller's side file and line with #file and #line parameters and include in the exception's body, but the problem is that for consistency such parameters should be added for all throwing ViewInspector's APIs, not just find. And this seems to be an overkill already.

I don't know how your CI is configured, but maybe a better course of action would be to make sure it logs the exception location automatically, as the Xcode seems to be capable of doing it.

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

No branches or pull requests

2 participants