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

Lesson 8 - Exception Handling Conundrum #3

Open
neyl opened this issue May 4, 2017 · 1 comment
Open

Lesson 8 - Exception Handling Conundrum #3

neyl opened this issue May 4, 2017 · 1 comment

Comments

@neyl
Copy link

neyl commented May 4, 2017

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
Copy link

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

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

No branches or pull requests

2 participants