0

我有这个代码:

public function checkKeyConstraint($key) {
    if ($this->hasKey($key)) {
        throw new Exception('Key already exists '.$key);
    }

    return $this;
}

我也有两种带有数据提供者的 PHPUnit 测试方法,一种用于存在的键,另一种用于不存在的键。问题是代码覆盖工具将该throw new Exceptions('Key already exists '.$key); 行显示为未执行

PHP_CodeCoverage 1.1.3、PHPUnit 3.6.12、PHP 5.4.4、xdebug 2.2.1-5.4-vc9

UPD:测试方法和数据提供者

/**
 * @covers            Dict::checkKeyConstraint
 *
 * @dataProvider      providerNonexistentKeys
 */
public function testCheckKeyConstraintNonExistent($key) {
    $this->assertEquals(self::$object, self::$object->checkKeyConstraint($key));
}

/**
 * @covers            Dict::checkKeyConstraint
 *
 * @expectedException Exception
 * @dataProvider      providerValidKeyValues
 */
public function testCheckKeyConstraintExistent($key) {
    $this->assertEquals(self::$object, self::$object->checkKeyConstraint($key));
    $this->fail();
}

public function providerNonexistentKeys() {
    $data = array();

    for ($i = 0; $i < 10; $i++) {
        $data[] = array('randKey:' . rand());
        $data[] = array(rand());
    }

    return $data;
}

public function providerValidKeyValues() {
    $data = array();

    for ($i = 0; $i < 20; $i++) {
        $stub   = new Key('id#' . $i, 'val#' . $i . '#string');
        $data[] = array($stub, $stub);
        $stub   = new Key($i, 'val#' . $i . '#numeric');
        $data[] = array($stub, $stub);
    }

    return $data;
}
4

1 回答 1

0

我是个白痴。问题是在这种testCheckKeyConstraintExistent方法中我检查整个对象,而不是它的关键。应该改写为:

public function testCheckKeyConstraintExistent(Key $key) {
    $this->assertEquals(self::$object, self::$object->checkKeyConstraint($key->_id()));
    $this->fail();
}
于 2012-08-13T19:52:24.897 回答