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

Replace 'not found' with 'internal server error' in assert_error_sent/2 examples #5481

Conversation

phoebe100
Copy link
Contributor

Replace 'not found' with 'internal server error' in assert_error_sent/2 code examples.
This could reduce confusion since "not found" errors are no re-raised.
Closes #5480

@luka-TU
Copy link

luka-TU commented Jun 2, 2023

@phoebe100 sorry, I thought, generally speaking, hitting a missing route should result in a 404 and not in Internal Server Error?

@phoebe100
Copy link
Contributor Author

hitting a missing route should result in a 404

True, and a running phoenix application will do that.

But when testing controllers, you should differentiate:
a) If you want to verify, that you get back a 404 for a non-existing route, simply use:

conn = get(build_conn(), "/users/not-found")
assert response(conn, 404)

b) If you want to verify, that some error was raised while processing the route and that it was linked to an expected status code, then use assert_error_sent/2

Since requesting unknown routes do not result in (re-)raising an error anymore, assert_error_sent/2is not the right function for testing unknown routes. This commit just tries to clean up the example code in the docs, so that people are not tempted to use that function for testing unknown routes.

@luka-TU
Copy link

luka-TU commented Jun 3, 2023

@phoebe100 I think I get what you are trying to say. What throws me off the the name of the method: assert_error_sent: it suggests that we should expect and error be sent when in reality it is to check if an error was raised. In python this naming is a bit more to the point imho, with assertRaises.

@phoebe100
Copy link
Contributor Author

it suggests that we should expect and error be sent when in reality it is to check if an error was raised

Actually, it's both. The intention of the function is to assert:

  • that an error was raised
  • that the error is wrapped in a response with a specific HTTP status
  • and a specific content (you can pattern match on the returned response tuple as mentioned in the docs)

So it's all about asserting error translation.

@luka-TU
Copy link

luka-TU commented Jun 6, 2023

@phoebe100 this is just my opinion, but not getting an error raised for a 404 in combination with this method name do cause some confusion.

@phoebe100 phoebe100 force-pushed the 5480_adjust_example_for_assert_error_sent branch 2 times, most recently from 5ef7338 to 482b607 Compare July 11, 2023 17:27
@phoebe100 phoebe100 force-pushed the 5480_adjust_example_for_assert_error_sent branch from 482b607 to d3b07bc Compare July 27, 2023 13:05
@stefanchrobot
Copy link
Contributor

not getting an error raised for a 404 in combination with this method name do cause some confusion.

@luka-TU Agreed, hence the original reported issue. I think the docs could still use the 404 example, but it should also have a clear note that NoRouteError is not re-raised and can't be tested with assert_error_sent (I think the note should be added regardless if the docs use the 404 or 500 example).

@luka-TU
Copy link

luka-TU commented Jul 31, 2023

@stefanchrobot the more detailed the docs are - the better, agree. At the same time, maybe the naming of these methods could have been a bit better, borrowing from some other languages like in the example above.

@chrismccord chrismccord merged commit 8cac3ec into phoenixframework:main Aug 2, 2023
4 checks passed
@chrismccord
Copy link
Member

❤️❤️❤️🐥🔥

@phoebe100 phoebe100 deleted the 5480_adjust_example_for_assert_error_sent branch August 2, 2023 21:31
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

Successfully merging this pull request may close these issues.

Documentation for assert_error_sent/2 does not match actual behavior
4 participants