【问题标题】:How do I make route model binding work in Laravel during unit tests?如何在单元测试期间使路由模型绑定在 Laravel 中工作?
【发布时间】:2017-12-11 11:25:48
【问题描述】:

我的大多数单元测试都使用无中间件,因此我可以测试控制器和资源端点。然而路由模型绑定是一个中间件,所以控制器没有得到他们需要的模型。

【问题讨论】:

    标签: laravel-5 phpunit laravel-middleware


    【解决方案1】:

    这是我的问题,可能不再相关。过去,laravel 测试可以使用中间件,也可以不使用中间件。因此,路由模型绑定将与其他所有内容一起打开或关闭。现在 Laravel 测试支持省略特定的中间件。所以你可以使用这样的东西......并保持其他中间件包括绑定的东西。

    namespace Tests\Feature;
    use App\Http\Middleware\VerifyCsrfToken;
    use ...
    use Illuminate\Foundation\Testing\DatabaseTransactions;
    use Illuminate\Foundation\Testing\WithFaker;
    use Illuminate\Foundation\Testing\RefreshDatabase;
    use Illuminate\Foundation\Testing\WithoutMiddleware;
    use Tests\TestCase;
    
    class BankDisplayControllerTest extends TestCase{
    
        use DatabaseTransactions;
    
        //use WithoutMiddleware;
    
        protected function setUp(){
            parent::setUp();
    
            $this->withoutMiddleware([VerifyCsrfToken::class]);
        }
       ...
    

    【讨论】:

      猜你喜欢
      • 2022-12-23
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 2019-06-06
      • 2021-05-24
      • 2016-06-04
      • 2021-03-15
      • 2020-11-21
      相关资源
      最近更新 更多