流明单元测试 post 请求

Lumen unit test post request

我在设置单元测试时遇到问题,我在 API 中收到 POST 请求。 我在 table 上与创建 POST 请求的用户应该 link 有关系。在 API 中,我 link 正确的用户是这样的: $record->user()->associate($req->user());

效果很好,但我似乎无法编写有效的单元测试。

我的单元测试是这样的

$user = factory('App\User')->create();
$item = factory('App\Item')->create();
$response = $this->actingAs($user, 'api')->post('/route', $item);

$this->assertEquals(200, $response->status());

我做错了什么?

问题是我在进行 API 调用之前在数据库中创建了项目。

$item = factory('App\Item')->create(); 应该是 $item = factory('App\Item')->make();$response = $this->actingAs($user, 'api')->post('/route', $item); 应该是 $response = $this->actingAs($user, 'api')->post('/route', $item->toArray());