2014-04-01 57 views
0

我一定要考類add_HookPHPUnit的模擬抽象父方法

class add_Hook extends Hooks_base 

我試圖創建hooks_base的像這樣的模擬:

> $this->oHookBaseMock = $this->getMock(
>      'Hooks_base', 
>       array('get_database','get_arguments'), 
>       array(null,'null') //this are the parameters for the constructor 
>  ); 


$this->hook->expects($this->any()) 
     ->method('get_database') 
     ->will($this->returnValue(true) 
    ); 

     $this->hook->expects($this->any()) 
     ->method('get_arguments') 
     ->will($this->returnValue($arraySImpleXmlObject) 
    ); 

,做這樣的事情:

$hook = new add_Hook($this->hook) 

現在我的問題是,當我運行測試它鋼要求我的父類的兩個參數(hooks_base)

構造是這樣的

public function __construct($parameter_1, $parameter_2) { 
    $this->_param1 = $parameter_1; 
    $this->_param2 = $parameter_2; 
} 

我不」知道如何禁用父類的構造函數,如果我這樣做。

+0

'add_hook'有 '私有函數的init(){$ DB = $這個 - > get_database; $ arguments = $ this-> get_arguments; }' 是的,我傳遞了兩個參數,但我不知道它爲什麼不能識別它們 – Nani

回答

0

我只是在我的測試中創建了一個抽象類的實例。 $ Object = new ABSTRACT_CLASS()並且在需要定義的方法的定義中沒有代碼,因爲我不打算測試抽象方法。

class TestAbstractInput extends AbstractInput 
{ 
    public ImplementAbstractFunction() 
    { // Purposely empty so nothing happens. This would be defined in normal class extending AbstractInput 
    } 
    ... 
} 

class AbstractInputTest extends TestCase 
{ 
    // ... 
    public function setUp() 
    { 
     $this->TestClassObject = new TestAbstractInput(); 
    } 

    public function test_isValid() 
    { 
     $this->assertEquals(1, $this->TestClassObject->isValid); 
    } 
} 

這樣可以測試單個方法。然後我測試通常擴展抽象類的普通類,以測試在該類中實現的函數。

class InputTest extends TestCase 
{ 
    // ... 
    public function setUp() 
    { 
     $this->TestClassObject = new AbstractInput(); 
    } 

    // Parent function should still work 
    public function test_isValid() 
    { 
     $this->assertEquals(1, $this->TestClassObject->isValid); 
    } 

    public function test_ImplementAbstractFunction() 
    { 
     $this->assertTrue($this->AbstractFunction()); 
     ... 
    } 
} 

編輯還有內置的getMockForAbstractClass()可以,如果你使用的是新版本足以爲你直接工作。您還可以指示Mocks不運行構造函數。

0

你不能「嘲笑」你的課程的父類。你可以創建你的類的模擬並替換你想要替換的父方法。

$add_hook = $this->getMock('add_Hook', 
          //This will mock these methods only and leave the rest untouched provided the class is able to be loaded. 
          array('get_database','get_arguments'), 
          array(param1, param2)); 
$this->hook->expects($this->any()) 
    ->method('get_database') 
    ->will($this->returnValue(true) 
); 

$this->hook->expects($this->any()) 
    ->method('get_arguments') 
    ->will($this->returnValue($arraySImpleXmlObject) 
); 

你的嘗試是莫名其妙地注入到父類。這個類是父類的一個實例,所以你不能在不嘲笑類本身的情況下「模擬」它。

在我看來,這不是一個好的做法。你正在測試你的類的實現,而不是測試你的類的行爲。你的測試應該檢查你的方法是做什麼,而不是如何做。什麼是'get_database'和'get_arguments'呢?也許你應該把'Hooks_base'的實例傳遞給你的類,而不是擴展它。

從你已經張貼我的測試看起來像:

public function testInit() { 
    $param1 = <what ever the argument needs to be>; 
    $param2 = <something else>; 

    $addHook = new add_Hook($param1, $param2); 

    $this->assertInstanceOf('Hooks_base', $addHook); //I would probably have this in a constructor test 

    $addHook->init(); 

    //Assertions for what init() does 
}