Php 如何在Laravel测试POST路由
我正在做以下测试,以测试对Laravel的POST调用。我希望,根据我的路线,将帖子发送至问题,作为门店行动方法。这在浏览器中起作用 我的测试:Php 如何在Laravel测试POST路由,php,laravel,phpunit,Php,Laravel,Phpunit,我正在做以下测试,以测试对Laravel的POST调用。我希望,根据我的路线,将帖子发送至问题,作为门店行动方法。这在浏览器中起作用 我的测试: public function setUp() { parent::setUp(); Session::start(); } public function testStoreAction() { $response = $this->call('POST'
public function setUp()
{
parent::setUp();
Session::start();
}
public function testStoreAction()
{
$response = $this->call('POST', 'questions', array(
'_token' => csrf_token(),
));
$this->assertRedirectedTo('questions');
}
但是,我告诉我重定向不匹配。另外,我可以看到它根本不会出现在StoreAction方法中。我想知道它将使用什么操作方法,以及为什么它不使用store方法(如果我查看route:list,我可以看到有一个POST问题/路线应该使用questions.store;这也可以在浏览器中使用,但在我的测试中不起作用)。另外,我是否正确编写了此资源的调用?我在这里添加了令牌,因为它应该抛出异常,在一些测试中,我会让令牌检查通过。测试路由的最推荐方法是检查
200
响应。当您有多个测试时,这非常有用,例如您一次检查所有的post
路由
要做到这一点,只需使用:
public function testStoreAction()
{
$response = $this->call('POST', 'questions', array(
'_token' => csrf_token(),
));
$this->assertEquals(200, $response->getStatusCode());
}
你可以试试这个:
public function testStoreAction()
{
Session::start();
$response = $this->call('POST', 'questions', array(
'_token' => csrf_token(),
));
$this->assertEquals(302, $response->getStatusCode());
$this->assertRedirectedTo('questions');
}
我得到了一个
标记不匹配异常
,这就解决了它,也许它也对你有帮助
public function testStoreAction()
{
$response = $this->withSession(['_token' => 'covfefe'])
->post('questions', [
'_token' => 'covfefe',
));
$this->assertRedirectedTo('questions');
}
没有中间件的Laravel单元案例 use WithoutMiddleware; protected $candidate = false; public function setUp(): void { parent::setUp(); $this->candidate = new Candidate(); } /** @test */ public function it_can_get_job_list() { $this->actingAs($this->user, 'api'); $response = $this->candidate->getJobsList(); $this->assertNotNull($response); $this->assertArrayHasKey('data', $response->toArray()); $this->assertNotEmpty($response); $this->assertInternalType('object', $response); } 不使用中间件; 受保护$candidate=false; 公用函数设置():无效 { 父::设置(); $this->candidate=新候选人(); } /**@测试*/ 公共功能it可以获得工作列表() { $this->actingAs($this->user'api'); $response=$this->candidate->getJobsList(); $this->assertNotNull($response); $this->assertArrayHasKey('data',$response->toArray()); $this->assertNotEmpty($response); $this->assertInternalType('object',$response); } 我用 以便断言验证工作。但是要使用它,您必须从发送post请求的页面开始。像这样:
$user = User::where('name','Ahmad')->first(); //you can use factory. I never use factory while testing because it is slow. I only use factory to feed my database and migrate to make all my test faster.
$this->actingAs($user)->get('/user/create'); //This part is missing from most who get errors "key errors is missing"
$response = $this->post('/user/store', [
'_token' => csrf_token()
]);
//If you use custom error message, you can add as array value as below.
$response->assertSessionHasErrors(['name' => 'Name is required. Cannot be empty']);
$response->assertSessionHasErrors(['email' => 'Email is required. Make sure key in correct email']);
然后,如果要测试错误是否也正确显示,请返回。再次运行上述测试,并进行如下更改:
$this->actingAs($user)->get('/user/create');
$response = $this->followingRedirects()->post('/user/store', [
'_token' => csrf_token()
]); //Add followingRedirects()
$response->assertSeeText('Name is required. Cannot be empty');
$response->assertSeeText('Email is required. Make sure key in correct email');
我的猜测是,如果您不从显示错误的页面开始(您放置表单的“创建/更新”页面),那么过程中的会话链将丢失一些重要的键。您能否提供实际的错误描述,谢谢。您的POST参数不太可能只是csrf令牌。可能在控制器中有某种验证,使请求重定向回以前的位置(302响应代码)。您可以通过使用
dd(session::get('errors'))
调试会话来检查是否存在这种情况,但此操作不会返回200代码,我正在重定向用户(30x),我想确认他们是否转到了正确的URL(问题)。
$this->actingAs($user)->get('/user/create');
$response = $this->followingRedirects()->post('/user/store', [
'_token' => csrf_token()
]); //Add followingRedirects()
$response->assertSeeText('Name is required. Cannot be empty');
$response->assertSeeText('Email is required. Make sure key in correct email');