我正在编写Laravel 5.4测试,我需要测试更新方法,我不确定在哪里传递$id
以进行编辑。测试商店方法时,我只使用了此
$this->json('POST', "/example/url", [(form input here)]
它可以正常工作,对于更新,我要这样做,
$this->json('PUT', "/example/url", [(form input here)]
,但是要使它工作,我需要通过id
来进行我想要编辑的记录。因此,理想情况下,我想使用工厂创建记录,然后在测试中调用更新方法后,我将断言该字段已被编辑。
$record = factory(Record::class)->create(['name' => 'New Name']);
$response = $this->json('PUT', "/example/url",
['name' => 'Edited Name']);
// I should pass the record->id here, I just don't know how
$this->assertDatabaseHas('records', ['name' => 'Edited Name']);
$this->assertDatabaseMissing('records', ['name' => 'New Name']);
应将id
作为URL的一部分传递,
例如;$this->json('PUT', "/example/url/{id}", [(form input here)]