【发布时间】:2011-05-07 15:53:04
【问题描述】:
所以我已经用 codeigniter (https://github.com/ericbarnes/codeigniter-simpletest) 设置了简单测试
我运行了一些简单的测试,一切看起来都不错。但我遇到了障碍。我不知道如何使用会话数据进行测试。
问题是,测试运行良好。但我遇到了一些例外情况。如果我单独运行我的测试(即除了 simpletest 中的“所有”选项卡之外的任何东西),那么就没有问题。当我运行“所有”测试时,我得到了这个错误:
意外的 PHP 错误 [无法修改标头信息 - 标头已由(输出开始于 */www/public/tests/simpletest/extensions/my_reporter.php:193)] 严重性 [E_WARNING] in [ */www/system/libraries/Session.php line 408]Blockquote
现在我猜这一切都在使用我的浏览器来设置会话,并且在第一个测试用例完成后您无法设置/取消设置它们(在我的情况下,有一个用户模型测试用例完成,然后第二个测试用例(认证库),有异常。
我猜第一个测试用例完成后,标头已经发送了。
<?php
class test_auth extends CodeIgniterUnitTestCase
{
public function __construct()
{
parent::__construct();
$this->UnitTestCase('Authorization Library');
$this->rand_good = rand(500,15000);
$this->rand_bad = rand(500,15000);
}
public function setUp()
{
$this->_ci->db->flush_cache();
$this->_ci->db->truncate('users');
$this->_ci->session->sess_destroy();
$this->_ci->session->unset_userdata('logged_in_id');
$u = new User();
$u->email = 'email' . $this->rand_good;
$u->password = 'pass' . $this->rand_good;
$u->confirm_password = 'pass' . $this->rand_good;
$u->save();
}
public function tearDown()
{
$u = new User();
$u->get();
foreach($u->all as $user)
{
$user->delete();
}
}
public function test_login_good_email_good_password()
{
$u = new User();
$u->email = 'email' . $this->rand_good;
$u->password = 'pass' . $this->rand_good;
$this->assertTrue($this->_ci->auth->login($u), 'login');
$this->assertTrue($this->_ci->auth->is_logged_in(), 'is logged in');
}
public function test_login_bad_email_bad_password()
{
$u = new User();
$u->email = 'email' . $this->rand_bad;
$u->password = 'pass' . $this->rand_bad;
$this->assertFalse($this->_ci->auth->login($u), 'login');
$this->assertFalse($this->_ci->auth->is_logged_in(), 'is logged in');
}
}
/* End of file test_auth.php */
影响这一点的两行是session->sess_destroy() 和session->unset_userdata()
tearDown() 或 setUp() 中的那些行将导致相同的问题。每一个都会导致一个头异常。
我想我希望我可以将浏览器部分从测试中去掉,并且那个 simpletest 可以以某种方式模拟它。
我能做些什么来解决这个问题?
【问题讨论】:
-
为初学者发布一些代码。我想如果您在控制器中输出内容,这将是原因。在任何关闭后还要检查空格?>。但请后控制器
-
我的代码中没有控制器。不过,我发布了测试课程,如果有帮助的话。
标签: php unit-testing codeigniter tdd simpletest