2

I have turned throw Exception in handler.php so that I can catch exceptions and see the errors but what happened is when I try to do the validation checks it throws me an exception which is correct but in my test case instead of catching the exception i'm asserting that the session has errors.

/** @test*/
    public function a_thread_requires_a_title(){

        $thread = make('App\Thread', ['title'=> null]);
        $this->post('threads', $thread->toArray())
            ->assertSessionHasErrors('title');
    }

Since, validation error is an exception so it throws me an exception because I've modified the handler.php file as

if(app()->environment() === "testing") throw $exception;

so, what I'm trying to do is change the env for this one test so that it wont throw me an 'Exception'

5
  • 1
    Put this at the top of your test method: - $this->withoutExceptionHandling(); Commented Mar 16, 2019 at 15:10
  • withoutExceptionhandling its already built in ? Commented Mar 16, 2019 at 15:55
  • @senty this method is not working for me.. I'm still getting the validation exception.? is there a way around? Commented Mar 16, 2019 at 16:00
  • Omg ..... there's withExceptionHandling as well.. sorry i was following old courses ... it was the opposite.. thanks a bunch man Commented Mar 16, 2019 at 17:45
  • :) No worries - happy coding Commented Mar 16, 2019 at 17:45

1 Answer 1

3

There are 2 helper methods which you can write at the top of your test method:

$this->withoutExceptionHandling(); and $this->withExceptionHandling();

They are included in Laravel's 'Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling' trait which is used by the abstract TestCase that you should be extending from your test. (as mentioned here)

/** @test*/
public function a_thread_requires_a_title() {
    $this->withExceptionHandling();

    $thread = make('App\Thread', ['title'=> null]);
    $this->post('threads', $thread->toArray())
        ->assertSessionHasErrors('title');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.