Yii 这是正确的方法吗

Yii 这是正确的方法吗,yii,phpunit,Yii,Phpunit,我今天和菲普尼特在一起。当我使用Yii框架时,我使用的是内置函数 有人能告诉我我是否进行得正确吗 下面是模型函数 public function getTaxRate() { if($this->province_id != 13 && $this->province_id != 14) { return 21; } elseif($this->identification[0] == 'B') {

我今天和菲普尼特在一起。当我使用Yii框架时,我使用的是内置函数

有人能告诉我我是否进行得正确吗

下面是模型函数

public function getTaxRate()
{
    if($this->province_id != 13 && $this->province_id != 14)
    {
        return 21;
    }
    elseif($this->identification[0] == 'B')
    {
        return 0;       
    }
    else
    {
        return 7;       
    }

} 
下面是测试用例

public function testgetTaxRate()
{
    $accountData = array(
                                array('identification'=>'x2', 'province_id'=>'50', 'result'=>21), // test for 21
                                array('identification'=>'x2', 'province_id'=>'13', 'result'=>7), // test for 7
                                array('identification'=>'B2', 'province_id'=>'13', 'result'=>0), // test for 0
                        );
    foreach($accountData as $account)
    {
        $acc = new Accounts();
        $acc->identification=$account['identification'];
        $acc->province_id=$account['province_id'];
        $tax = $acc->getTaxRate();
        $this->assertEquals($tax, $account['result']);
    }
}
如果我做得正确,结果是正确的,并且在我预期的时候它会出错


关于

组织测试有一个很好的规则:每个案例一次测试。如果您只有一个逻辑,则应为此目的使用数据提供程序机制(由
PHPUnit
提供),以避免代码重复

文件:

给你举个例子:

/**
 * @dataProvider dataProvider_getTaxRate
 */
public function testGetTaxRate($id, $provinceId, $expectedResult)
{
    $acc = new Accounts();
    $acc->identification = $id;
    $acc->province_id = $provinceId;

    $this->assertEquals($expectedResult, $acc->getTaxRate());
}

public static function dataProvider_getTaxRate()
{
    return array(
        array('x2', '50', 21),
        array('x2', '13', 7),
        array('x2', '14', 7),
        array('B2', '13', 0),
    );
}

还有一件事——在断言的第一个参数中,期望值应该是一个好习惯。

你能在这里发布错误吗Hiya,当我说它是错误时,我的意思是如果我破坏了方法,测试会失败,因为它应该是抱歉的。非常感谢,这就是我现在编写下几个测试的方式。