SyntaxHighlighter

About Me

2016年7月14日 星期四

Laravel PhpUnit 關閉 middleware

Laravel Unit test 筆記

Laravel PhpUnit 關閉 middleware

方法一

  • 修改 route 取 middle ware 的方式

於使用 middleware 之前加上

// app/Http/route.php

$middleware = [ ];
if ( !App::runningUnitTests() ) {
    $middleware[] = 'api.auth';
}

$api->version( 'v1', function ( $api )  use ($middleware){
    $api->get( '/users/me', ['middleware' => $middleware,
        'uses' => '\App\Http\Controllers\UserController@currentUser'
     ]);
});

Ref: https://github.com/dingo/api/issues/571#issuecomment-129939376

方法二

  • 於 test case 中加上 use WithoutMiddleware

透過這個方式也會跳過 middleware

<?php

    use Illuminate\Foundation\Testing\WithoutMiddleware;
    use Illuminate\Foundation\Testing\DatabaseMigrations;
    use Illuminate\Foundation\Testing\DatabaseTransactions;

    class ExampleTest extends TestCase
    {
        use WithoutMiddleware;
        
        public function test_example() {
                ...
        }
    }

Ref: https://laravel.com/docs/5.1/testing#application-testing

沒有留言:

張貼留言