【问题标题】:Error for db select with phpunit and Zend Framework使用 phpunit 和 Zend Framework 选择 db 时出错
【发布时间】:2013-04-16 21:33:23
【问题描述】:

我想在 ZF 项目中使用单元测试,所以我创建了测试类,但出现了错误:

致命错误:对 ...\CardMapper.php 中的非对象调用成员函数 select()

有关的行是:

$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();

$select = $this->db->select()->from(array('c' => 'cards'));

而且我用浏览器也没有问题。

这是我的 phpunit.xml(来自另一个没有数据库的 zf 项目):

<phpunit bootstrap="./bootstrap.php">
    <testsuite name="Application Test Suite">
        <directory>./application</directory>
    </testsuite>
    <testsuite name="Library Test Suite">
        <directory>./library</directory>
    </testsuite>

    <filter>
        <whitelist>
            <directory suffix=".php">../../library/Zend</directory>
        </whitelist>
    </filter>
</phpunit>

bootstrap.php:

<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

还有测试课:

<?php

class CardControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{

    public function setUp()
    {
        $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
        parent::setUp();
    }

    public function testList()
    {
        $this->dispatch('/card');
    }
}

我尝试了很多东西,但都没有真正奏效。某些代码将错误更改为“未找到默认模块”。

感谢您的帮助:)

【问题讨论】:

    标签: zend-framework phpunit


    【解决方案1】:

    好的,我发现了问题:

    在application.ini中,我只放了开发环境的数据库配置,而单元测试仍在使用测试环境配置。

    所以问题解决了!

    【讨论】:

      猜你喜欢
      • 2017-06-27
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 2010-12-31
      • 2015-07-26
      • 1970-01-01
      相关资源
      最近更新 更多