有许多测试工作,但我对某些对象的实际测试失败了,我不知道为什么。当我在一个简单的测试中单步执行代码时,我可以看到我要测试的内容已定义,但 PHPUnit 未能通过测试。
我在下面编辑了一些小代码示例,以尝试在没有太多噪音的情况下说明问题。
类定义的非常简单的子集。
class UTIL_CATEGORY_SCOPE extends UTIL_MESSAGE_DATA
{
function __construct($CategoryNo = NULL, $CategoryName = NULL)
{
$this->DeclareClassFields_();
$this->CategoryName = $CategoryName;
$this->CategoryNo = $CategoryNo;
}
private function DeclareClassFields_()
{
$this->Fields['CategoryNo'] = new UTIL_ICAP_FIELD('CCL', 6, 'Category', 8);
$this->Fields['SubCategoryNo'] = new UTIL_ICAP_FIELD('SCC', 6, 'Sub-Category', 12);
$this->Fields['SubSubCategoryNo'] = new UTIL_ICAP_FIELD('SSC', 6, 'Sub-Sub-Category', 12);
}
}
Netbeans 调试器中的普通代码可以查看此功能。
$Category = new UTIL_CATEGORY_SCOPE();
转储内容以比较 PHPUnit 测试样本。
var_dump($Category);
class UTIL_CATEGORY_SCOPE#1 (4) {
public $TheMessage_ =>
class MESSAGE_DATA#2 (0) {
}
还行吧。预期和工作,因为类 InstanceOf 可以被测试并正确通过。
在内部,有一个在数组中维护的字段列表,这是一个类对象数组,它使用魔术方法来获取/设置值等...... 数组是否已初始化?
var_dump($Category->Fields);
array(3) {
'CategoryNo' =>
class UTIL_ICAP_FIELD#3 (14) {
public $FieldCode =>
string(3) "CCL"
public $FieldLength =>
int(6)
public $FieldTitle =>
string(8) "Category"
}
'SubCategoryNo' =>
class UTIL_ICAP_FIELD#4 (14) {
public $FieldCode =>
string(3) "SCC"
public $FieldLength =>
int(6)
public $FieldTitle =>
string(12) "Sub-Category"
}
'SubSubCategoryNo' =>
class UTIL_ICAP_FIELD#5 (14) {
public $FieldCode =>
string(3) "SSC"
public $FieldLength =>
int(6)
public $FieldTitle =>
string(16) "Sub-Sub-Category"
}
}
数组按预期初始化。
检查数组是否存在并且可以访问各个项目。
if (array_key_exists('CategoryNo', $Category->Fields))
echo 'Array Key Exists';
Array Key Exists
if( $Category->Fields['CategoryNo'] instanceof UTIL_ICAP_FIELD )
echo 'Yes';
Yes
一切都按预期报告。
PHPUnit 测试部分:
class TEST_UTIL_CATEGORY_SCOPE extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
}
public function testObjectCreation()
{
$CategoryInfo = new UTIL_CATEGORY_SCOPE();
$this->assertInstanceOf('UTIL_CATEGORY_SCOPE', $CategoryInfo);
$this->assertInstanceOf('UTIL_DATA_STRUCTURE', $CategoryInfo);
}
public function testConstructFieldOrder()
{
$CategoryInfo = new UTIL_CATEGORY_SCOPE(1500, 'Category Name');
$this->assertEquals(1500, $CategoryInfo->CategoryNo);
$this->assertEquals('Category Name', $CategoryInfo->CategoryName);
}
/**
*
*/
public function testConstructDefaults()
{
$CategoryInfo = new UTIL_CATEGORY_SCOPE();
$this->assertNull($CategoryInfo->CategoryNo);
$this->assertNull($CategoryInfo->CategoryName);
}
这些测试按预期通过。但是,鉴于上述信息的转储,以下 2 个测试失败,即使测试运行和 var_dump 显示这些值已按预期初始化并呈现。
public function testFieldsCreated()
{
$CategoryInfo = new UTIL_CATEGORY_SCOPE();
$this->assertArrayHasKey('CategoryNo', $CategoryInfo->Fields);
$this->assertArrayHasKey('SubCategoryNo', $CategoryInfo->Fields);
$this->assertArrayHasKey('SubSubCategoryNo', $CategoryInfo->Fields);
}
创建的错误:
1) TEST_UTIL_CATEGORY_SCOPE::testFieldsCreated
Failed asserting that an array has the key 'CategoryNo'.
下一个测试
public function testICAPFieldTypes()
{
$CategoryInfo = new UTIL_CATEGORY_SCOPE();
$this->assertInstanceOf('UTIL_ICAP_FIELD', $CategoryInfo->Fields['CategoryNo']);
$this->assertInstanceOf('UTIL_ICAP_FIELD', $CategoryInfo->Fields['SubCategoryNo']);
$this->assertInstanceOf('UTIL_ICAP_FIELD', $CategoryInfo->Fields['SubSubCategoryNo']);
}
创建的错误:
2) TEST_UTIL_CATEGORY_SCOPE::testICAPFieldTypes
Failed asserting that null is an instance of class "UTIL_ICAP_FIELD".
我不知道如何继续,因为当我看到对象已创建时,这会导致错误,数组填充如我所料。我现在必须将这些测试标记为未完成,以允许我们的测试套件继续进行。
有没有人有任何想法?