laracasts / Lets-Build-a-Forum-in-Laravel

http://laracasts.com/series/lets-build-a-forum-with-laravel
914 stars 309 forks source link

Lesson 8 - Exception Handling Conundrum #3

Open neyl opened 7 years ago

neyl commented 7 years ago

I don't know if this is your doing but a new trait has been added to solve this in Master https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Testing/Concerns/InteractsWithExceptionHandling.php (I also have no access to Disqus so I don't know if this was pointed out on site)

To solve the issues in the video for Laravel 5.4. 1)Download that file and place in relevant directory (if you are not using master in composer - otherwise composer update might work). Alternatively, you can play around with namespace etc. and dump it where you like. 2) Import Trait and use Trait. 3) Remove if(app()->environment() === 'testing') throw $exception; from Handler::render method. 4) Only for tests that require the exceptions to be thrown - i.e. guests_may_not_create_threads() test in CreateThreadsTest you should add $this->withoutExceptionHandling(). But guests_cannot_see_the_create_thread_page() needs no touching up.

So this is result: `<?php

namespace Tests\Feature;

use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling;

class CreateThreadsTest extends TestCase { use DatabaseMigrations; use InteractsWithExceptionHandling;

/** @test */
function guests_may_not_create_threads()
{
    $this->withoutExceptionHandling()
         ->expectException('Illuminate\Auth\AuthenticationException');

    $thread = make('App\Thread');

    $this->post('/threads', $thread->toArray());
}

/** @test */
public function guests_cannot_see_the_create_thread_page()
{
    $this->get('threads/create')
        ->assertRedirect('/login');
}

`

and the tests went green!

mackhankins commented 7 years ago

@neyl Haha, I thought I saw this in Taylor's talk, but I couldn't find it. Thanks