我正在尝试测试在UsersController中的cakephp添加方法,我在UsersController中编写了以下操作
public function testAdd(): void
{
$this->enableCsrfToken();
$this->enableSecurityToken();
$user = ['username'=>'jone','email'=>'[email protected]', 'password'=>'123'];
$this->post('users/add',$user);
$this->assertRedirect('/users');
$count = $this->Users->find('all')->where($user)->count();
$this->assertEquals(1,$count);
}
运行此测试用例后,我收到以下错误
Time: 00:00.133, Memory: 14.00 MB
There was 1 failure:
1) App\Test\TestCase\Controller\UsersControllerTest::testAdd
Failed asserting that response has header 'Location'.
/Applications/XAMPP/xamppfiles/htdocs/cake4/vendor/cakephp/cakephp/src/TestSuite/IntegrationTestTrait.php:833
/Applications/XAMPP/xamppfiles/htdocs/cake4/tests/TestCase/Controller/UsersControllerTest.php:89
我将如何解决这个问题?
删除后
$this->assertRedirect('/users');
此脚本工作正常。在这里我将如何测试操作重定向是否正常?
注意:这里我使用蛋糕服务器运行 cakephp 应用程序,通过以下命令
bin/cake server
/users
的位置是http://localhost:8765/users
您的代码可能会引发异常,可能返回错误代码 500。