我正在尝试添加自定义断言TestReponse https://laravel.com/api/5.5/Illuminate/Foundation/Testing/TestResponse.html类所以我可以做这样的事情:
$response = $this->json('POST', '/foo/bar');
$response->myCustomAssertion();
我尝试创建一个App\TestResponse
扩展原始类,然后将其绑定到App\Provider\AppServiceProvider
class.
public function register()
{
$this->app->bind('Illuminate\Foundation\Testing\TestResponse', function ($app) {
return new App\TestResponse();
});
}
But $response->json()
仍然返回原始的而不是我自己的实现。
我怎样才能延长TestResponse
class?
如果你想要更细粒度的控制,你还可以扩展Illuminate\Foundation\Testing\TestResponse
,就像你所做的那样,然后覆盖createTestResponse
方法在你的TestCase
类返回自定义响应类的实例:
// Laravel 8 and above
protected function createTestResponse($response)
{
return tap(App\TestResponse::fromBaseResponse($response), function ($response) {
$response->withExceptions(
$this->app->bound(LoggedExceptionCollection::class)
? $this->app->make(LoggedExceptionCollection::class)
: new LoggedExceptionCollection
);
});
}
// Before Laravel 8
protected function createTestResponse($response)
{
return App\TestResponse::fromBaseResponse($response);
}
From Illuminate\Foundation\Testing\Concerns\MakesHttpRequests https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)