PHPUnit,使用类const更改状态 [英] PHPUnit, Using class consts to change State

查看:100
本文介绍了PHPUnit,使用类const更改状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Zend和PHPUnit.

I'm learning Zend and also PHPUnit.

这是我下面的内容



public function changeToIllegalState()
{
    return array(
        array( Application_Model_SomeModel::FAIL ),
        array( Application_Model_SomeModel::SUCCESS )
    );
}

/**
 * @dataProvider changeToIllegalState
 * @expectedException IllegalStateChangeException
 */

public function testIllegalStateChangeGeneratesException( $state )
{
    $mapper = new Application_Model_Mapper_SomeModel();
    $model = new Application_Model_SomeModel();

    $model->changeState( $state );

    $mapper->save( $model );

}

因此,正如您在此处看到的那样,数据提供者提供了一些常量,这些常量表示与模型不同的状态.

So as you can see here, the data provider provides some constants that represent different states from the model.

PHPUnit表示无法在dataprovider方法中找到Model类.但是,如果我尝试在测试方法中使用常量,则所有方法都可以正常工作,并且没有任何问题.我正在使用Zend自动加载器来加载我的类,到目前为止,一切都还不错.我知道我可以自己输入常量的值,但我不知道为什么会出现此错误.

PHPUnit says that it can't find the Model class in the dataprovider method. However, if I try to use the constants within the test methods, it all works and there are no problems. I'm using the Zend autoloader to load my classes and it has all been dandy up till now. I know that I could just put in the values for the constants themselves, but I don't know why I'm getting this error.

我只能假设在调用setup方法之前先调用dataprovider方法,因为我在setup方法中进行了所有自动加载业务.

I can only assume that the dataprovider methods are called before the setup method is called because I do all the autoloading business in the setup method.

我也尝试了以下方法,但仍然无法在const类上使用.

I have also tried the following but it still won't work with the class consts.




protected $_FAIL;
protected $_model;

public function setUp()
{
    parent::setUp();
    $this->_model = new Application_Model_SomeModel();
    $this->_FAIL = Application_Model_SomeModel::FAIL;
}

现在,当我尝试在提供者方法中使用$ _FAIL时,我得到的是NULL值,而不是我期望的'fail'字符串.这真的很奇怪.

Now, when I try to use $_FAIL in the provider method I get a NULL value instead of the 'fail' string that I'm expecting. This is really weird.

推荐答案

PHPUnit实例化将在运行任何测试之前运行的所有测试用例.

PHPUnit instantiates all of test cases that will be run before running any tests.

  • 每个测试方法一个,没有数据提供程序.
  • 每个数据提供者方法一个.
  • 每个数据提供程序返回的每个参数数组一个,因此返回四个数组的数组的提供程序将获得四个测试用例实例.

假设您要在bootstrap.php中设置自动加载器,它将加载包含这些常量的类.但是,我将尝试进行测试以查看:

Assuming you're setting up the autoloader in your bootstrap.php, it should load the class containing those constants. However, I would try a test to see:

public function changeToIllegalState()
{
    require_once 'Zend/Loader/Autoloader';
    Zend_Loader_Autoloader::getInstance();

    return array(
        array( Application_Model_SomeModel::FAIL ),
        array( Application_Model_SomeModel::SUCCESS )
    );
}

或者Zend Framework是将模型的目录添加到测试用例的setUp()方法之一的include路径中吗?

Or is Zend Framework adding the models' directory to the include path in one of the test case's setUp() method instead?

这篇关于PHPUnit,使用类const更改状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆