扩展另一个类的测试类(PHPUNIT)

时间:2014-04-01 14:24:32

标签: php unit-testing phpunit

我在phpUnit中测试时遇到问题,我有"class class_A extends Class_B"

并且我不知道如何测试,我试图像这样模拟b类

$this-> mock = $this-> getmock('class_A',array('Class_B'));

但是当我运行测试时,测试失败并返回此

  

缺少class_b :: __ construct()的参数1,

任何人都知道我该怎么做?

(对不起,如果我的英语很糟糕!!)

3 个答案:

答案 0 :(得分:1)

您可以按照docs

中的说明禁用构造函数
$stub = $this->getMockBuilder('Class_A')
                 ->disableOriginalConstructor()
                 ->getMock();

答案 1 :(得分:1)

我认为这就是你要找的东西:

assertInstanceOf

(参见:https://stackoverflow.com/a/7680164/3454966

编辑:如果您只想测试继承,请使用构造函数中的任何有效参数。

答案 2 :(得分:0)

我测试该类是否正确继承,并且父函数在那里。

protected $TestObject;

protected function setUp()
{
    $this->TestObject = new MyLib\Class_B;      // Namespaces being used?
}

public function testClassInstance()
{
    $this->assertInstanceOf('MyLib\Class_A', $this->TestObject);
    $this->assertInstanceOf('MyLib\Class_B', $this->TestObject);
}

// For Instance: These are from the Class A that was extended
public function testClassAWorks()
{
    $this->assertEquals('String', $this->TestObject->GetString(), 'Ensure the Class_A inherited function is returning String');
    $this->assertFalse($this->TestObject->IsFalseFunction());
}

// These are from the Class B
public function testClassBWorks()
{
    $this->assertEquals('Number', $this->TestObject->GetString2(), 'Ensure the Class_B function is returning Number');
    $this->assertTrue($this->TestObject->IsTrueFunction());
}