session_regenerate_id() - 已在单元测试 Yii 控制器中发送的标头
我正在尝试对我的控制器(Yii 框架)进行单元测试。
/**
* @dataProvider provider
*/
public function testActionEdit_view_login($controller){
$user = new CWebUser;
$user->id = 978;
$identity = new UserIdentity('[email protected]', '123456');
$user->login($identity);
$controller->actionEdit();
$output = ob_get_contents();
assertContains('Add/Change Profile Picture:', $output);
assertContains('bio', $output);
assertContains('specialties', $output);
assertContains('change login', $output);
assertContains('New Password', $output);
}
当我
$user->login($identity);
为了登录而这样做时,出现以下错误:
session_regenerate_id(): Cannot regenerate session id - headers already sent
我已经尝试通过将其放在类的开头来缓冲输出:
public static function setUpBeforeClass(){
ob_start();
}
我还将 ob_clean() 放在 setUp() 中,将 ob_end_clean() 放在tearDownAfterClass() 中。
我仍然收到标头已发送的消息。文件中没有空格或换行符,当我注释掉具体的测试方法时,它工作得很好。 login() 似乎导致了这个问题。
有人知道如何防止这种情况/也许对控制器进行不同的单元测试吗?
谢谢, B先生
I'm trying to unit-test my controller (Yii framework).
/**
* @dataProvider provider
*/
public function testActionEdit_view_login($controller){
$user = new CWebUser;
$user->id = 978;
$identity = new UserIdentity('[email protected]', '123456');
$user->login($identity);
$controller->actionEdit();
$output = ob_get_contents();
assertContains('Add/Change Profile Picture:', $output);
assertContains('bio', $output);
assertContains('specialties', $output);
assertContains('change login', $output);
assertContains('New Password', $output);
}
When I do
$user->login($identity);
in order to login, I get the following error:
session_regenerate_id(): Cannot regenerate session id - headers already sent
I've already tried buffering the output by putting this at the beginning of the class:
public static function setUpBeforeClass(){
ob_start();
}
I also put ob_clean() in setUp() and ob_end_clean() in tearDownAfterClass().
Still I get the message that headers have already been sent. There are no spaces or newlines in the file, when I comment out the specific test method, it works perfectly. The login() seems to cause the problem.
Anyone got ideas how to prevent this / maybe unit-test the controller differently?
Thanks,
MrB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在调用
$user->login
之前,添加以下代码:这会使用不执行任何操作的方法覆盖
regenerateID
方法。将 ob_start() 添加到引导程序也可以,但在一切完成之前 PHPUnit 不会输出。
使用这种方法,您仍然可以看到正在发生的进度。
我从 Yii 1.1.7 升级到 1.1.10,并在 1.1.8 中添加了
regenerateID
方法,所以今天遇到了这个错误。Before your call to
$user->login
, add the following code:This overwrites the
regenerateID
method with a method that does nothing.Adding
ob_start()
to the bootstrap also works, but there is no output from PHPUnit until everything has completed.With this method, you can still see the progress as it is happening.
I upgraded from Yii 1.1.7 to 1.1.10 and the
regenerateID
method was added in 1.1.8 so I got this error today.知道了。我在 ob_start() 之前包含了一些 Yii 文件,它们似乎已经打印了标题。现在我也缓冲它。
Got it. I had included some Yii files before the ob_start(), which seem to have printed the headers. Now I buffer that, too.