2017-04-07 128 views
1

我有一個文件,一個包括像這樣的:讓phpunit忽略include?

file.php

include(dirname(__FILE__)."/anotherfile.php"); 
class file { 
    function myfunction() { 
     $class = new anotherfile(); 
     $class->functionfromanotherfile(); 
    } 
    function myfunctionToBeTested() { 
     // do things here 
    } 
} 

我使用特拉維斯慈運行我與PHPUnit測試,問題是,我不提交「anotherfile.php」(具有連接到數據庫的邏輯和證書),這使得我試圖在測試腳本中包含此文件時無法工作。

tests.php

class tests extends PHPUnit_Framework_TestCase { 
    public function test_function() { 
     include(dirname(__FILE__)."/file.php"); 
     $class = new file(); 
    } 
} 

我可以做另外一個文件,只有我需要測試,並用它去,但也許你們可以拿出其他awnser功能。

回答

0

使用某種dependency injection

  • 修改要測試,使其接受憑據作爲構造函數的參數的類。
  • 在測試中,創建僞造或真實憑據並將它們傳遞給您的類的構造函數。