【问题标题】:php (codeigniter) - simpletest unit testing w/ session dataphp (codeigniter) - 带有会话数据的简单测试单元测试
【发布时间】: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-&gt;sess_destroy()session-&gt;unset_userdata()

tearDown() 或 setUp() 中的那些行将导致相同的问题。每一个都会导致一个头异常。

我想我希望我可以将浏览器部分从测试中去掉,并且那个 simpletest 可以以某种方式模拟它。

我能做些什么来解决这个问题?

【问题讨论】:

  • 为初学者发布一些代码。我想如果您在控制器中输出内容,这将是原因。在任何关闭后还要检查空格?>。但请后控制器
  • 我的代码中没有控制器。不过,我发布了测试课程,如果有帮助的话。

标签: php unit-testing codeigniter tdd simpletest


【解决方案1】:

sess_destroy() 方法设置了一个修改标头信息的新 cookie。因此,您应该只使用unset_userdata() 并清除您知道需要清除的字段才能正确完成测试。然后避免在测试中调用sess_destroy()

在此处查看setcookie() 电话:http://bitbucket.org/ellislab/codeigniter/src/c39315f13a76/system/libraries/Session.php#cl-401

【讨论】:

  • 是的,但即使是 unset_userdata() 也会导致同样的异常。我的理论是,每个测试用例都会一个接一个地运行并输出结果,而不是最后的所有结果,从而导致第一个测试用例能够控制标题。
猜你喜欢
  • 2013-05-14
  • 2010-11-09
  • 2015-07-25
  • 2018-11-24
  • 2011-09-26
  • 1970-01-01
  • 2019-07-21
  • 2014-07-03
  • 1970-01-01
相关资源
最近更新 更多