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

[5.5] Throw exception for non http exceptions when running tests #20724

Closed

Conversation

sileence
Copy link
Contributor

I'm having a problem when the following test throws a non-http exception:

        $this->post('profile', [
            'bio' => '',
            'twitter' => 'sileence'
        ])
        ->assertSee('Profile updated');

For example I had his route that throws a BadMethodCallException with the message Method [validateAlphaNumeric] does not exist. (the rule alpha_numeric does not exist, it is alpha_num).

Route::post('profile', function () {
    request()->validate([
        'twitter' => 'alpha_numeric',
    ]);

    return 'Profile updated';
});

But the test still passes, for some reason! I think it has to do with the long HTML generated by Whoops. If I deactivate Whoops to render the exception with Symfony, the test fails but I still get a long HTML dumped in the console.

I spent a couple of hours trying different solutions and I think what makes the most sense here is to throw the exception when it is not a HTTP exception. So the test fails and the dev gets to see the actual error.

This shouldn't affect validation exceptions and other http exceptions.

I am aware of with[out]ExceptionHandling the problem is that I might be actually trying to test the validation (and therefore will need exception handling enabled) for example:

        $this->withExceptionHandling()->post('profile', [
            'twitter' => '@{sileence}'
        ])
        ->assertSessionHasErrors('twitter');

This test fails, I get: Session is missing expected key [errors]. I however don't get the real reason why it failed (the method validateAlphaNumeric doesn't exist). I then have to go back to the test and call withoutExceptionHandling to understand what is going on.

I could even get a false positive if for some reason I use ->assertSessionMissing('errors'); instead of assertSessionHasErrors.

With this PR I will get the exception (even if exception handling is enabled).

Or maybe I am terrible wrong / missing something, in this case I appreciate if someone helps me to clarify this.

@sileence sileence force-pushed the throw_exception_in_unit_tests branch from f10fc3d to 36fbd7e Compare August 24, 2017 11:34
@taylorotwell
Copy link
Member

Combine your test with a test for the proper status code. Adding this will break other test cases.

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.

None yet

2 participants