Php 使用$this->;测试编辑功能时传递id的位置;JSON(';PUT';,';url';,[]))

Php 使用$this->;测试编辑功能时传递id的位置;JSON(';PUT';,';url';,[])),php,laravel,phpunit,laravel-5.4,Php,Laravel,Phpunit,Laravel 5.4,我正在编写Laravel 5.4测试,我需要测试更新方法,我不确定在哪里传递要编辑的记录的$id。在测试store方法时,我只使用了这个 $this->json('POST', "/example/url", [(form input here)] 它很好用,为了更新,我这样做 $this->json('PUT', "/example/url", [(form input here)] 但是为了让它工作,我需要为我想要编辑的记录传递一个id。因此,理想情况下,我希望使用工厂创建一

我正在编写Laravel 5.4测试,我需要测试更新方法,我不确定在哪里传递要编辑的记录的
$id
。在测试store方法时,我只使用了这个

$this->json('POST', "/example/url", [(form input here)]
它很好用,为了更新,我这样做

$this->json('PUT', "/example/url", [(form input here)]
但是为了让它工作,我需要为我想要编辑的记录传递一个
id
。因此,理想情况下,我希望使用工厂创建一个记录,然后在测试中调用update方法后,我将断言字段已被编辑

$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的一部分传递


e、 g
$this->json('PUT',“/example/url/{id},[(此处表单输入)]

我认为这取决于您的路线定义,我希望它是
”/dashboard/research/reports/“.$record->id
但是,这取决于您如何定义死记硬背。是的,我使用了
Route::resource
,我不敢相信我没有想到这一点。尽管您不需要
仅此
“/dashboard/research/reports/$record->id”
无论哪种方式,我都在
/
处结束了字符串,然后连接了
$record->id
,您只需将它放在字符串本身中即可(它会弄乱我的IDE,因此我选择连接)