我在控制器中有一个方法,我想测试
public function destroy(Instrument $instrument)
{
$instrument->delete();
flash()->success('Instrument Deleted Successfully!');
return Redirect::route('instrument.index');
}
如果我手动测试控制器方法工作正常。
然而,我一直试图测试这个没有太多运气... 顺便说一句,这个方法是通过DELETE方法访问的。
以下是我尝试过的一些调用示例:
$this->action('DELETE', 'InstrumentController@destroy', ['id' => 19]);
这就是..
$this->delete('/instrument/destroy/19');
我试图使用$ this-> visit()进行测试但是当你点击删除按钮时会出现一个bootstrap工具包,因为它没有在dom中预加载我无法使用$ this-> visit()进行测试...
如果有人可以提供帮助,那就太棒了
答案 0 :(得分:1)
你可以这样测试:
public function testDelete()
{
$response = $this->call('DELETE', 'instrument/19');
$this->assertEquals(200, $response->getStatusCode());
}
如果您需要提供csrf令牌,只需将其作为call
方法的第三个参数传递给数组
['_token' => csrf_token()]