【问题标题】:use pre created Slim environment in different methods以不同的方法使用预先创建的 Slim 环境
【发布时间】:2015-09-18 23:36:11
【问题描述】:

我正在用 phpunit 为几个类编写一些测试。每个类都有以下方法:

  function getDbh() {
    if ($this->dbh === null){
      $this->dbh = Slim::getInstance()->db->getConnection();
    }
    return $this->dbh;
  }

但问题是,在第一次测试 slim 之后,创建了这个 Environment 单例,我不知道我可以在以下测试中使用它。

为了让我的具体问题更清楚一点,我的每个测试类都有这个方法:

public function testGetDbh_dbhIsNull()
{
    $fixture = new testedClass();

    $app = new Slim();
    $DB = $this->getMockBuilder('DB')
                 ->disableOriginalConstructor()
                 ->getMock();
    $DB->method('getConnection')->willReturn('connection');
    $app->db = $DB;

    $this->assertEquals($fixture->getDbh(), 'connection');
}

但从第二次测试开始,由于以下错误,测试失败:

1) GroupTest::testSlim
Failed asserting that 'connection' matches expected null.

知道如何在每个测试中使用 Slim 单例吗?谢谢

【问题讨论】:

    标签: php phpunit slim


    【解决方案1】:

    你的测试错了:)

    public function testGetDbh_dbhIsNull()
    {
        $fixture = new testedClass();
    
        $app = new Slim();
        $DB = $this->getMockBuilder('DB')
                     ->disableOriginalConstructor()
                     ->getMock();
        $DB->method('getConnection')->willReturn('connection');
        $app->db = $DB;
    
        //This points to the wrong object... This should fix it
        $this->assertEquals($app->db, 'connection');
    }
    

    【讨论】:

    • 您的答案testedClass::getDbh() 究竟在哪里被测试?你不叫它......
    猜你喜欢
    • 2021-11-29
    • 2019-07-18
    • 2022-08-21
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    相关资源
    最近更新 更多