我有一个 Controller ,它正在通过构造函数注入(inject) Illuminate\Http\Request
的实例。现在我需要编写一个单元测试来测试 Controller 中使用 Request
实例中的值的调用。我决定使用Faker
。如何使用 Faker 生成关联数组,以便我可以在测试用例中使用该数组,例如
$this->post('the_uri','MyFakerArray')
动态数组将自动在我的 Controller 请求中可用。
最佳答案
无需伪造/模拟 Request 对象。 当您模拟请求时,laravel 会为您执行此操作,它会创建一个针对您请求的 url 的请求并传递变量,然后您从应用程序中获取响应。
例如:
$response = $this->call('POST', '/user', ['name' => 'Taylor']);
现在 $response
变量具有要测试的数据。
我认为你有:
getContent() // for getting the reponse body
getCode() // for http code: 200, 401 etc
当你这样做时,你的测试应该与响应一起工作,在这种情况下你不需要 Faker。
关于php - 测试 Laravel Controller - 伪造 Illuminate\Http\Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35431373/