【问题标题】:Lumen unit test post request流明单元测试发布请求
【发布时间】:2018-04-30 05:23:49
【问题描述】:

我在设置单元测试时遇到问题,在该单元测试中我在 API 中收到了一个 POST 请求。 我在表上有一个关系,应该链接到创建 POST 请求的用户。在 API 中,我像这样链接正确的用户: $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());

我在这里做错了什么?

【问题讨论】:

  • 测试结果如何?顺便说一句,我不认为这是一个单元测试,看起来更像是一个功能测试。
  • Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails 数据库中的用户 ID 失败
  • 将测试结果添加到您的问题中。如果您显示dd($req->user()) 的结果,也会很有用。

标签: laravel unit-testing lumen


【解决方案1】:

问题是我在进行 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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多