2016-05-01 124 views
0

我無法正確測試引發的異常(它是否爲空間問題)。PHPUnit未能測試自定義異常

這裏是我的自定義異常:

<?php 
namespace Example\Customexception; 

class ResourceNotAvailableException extends \Exception {} 

在這裏,類我測試:

namespace Example\Lib; 

use Example\Customexception\ResourceNotAvailableException; 

class Configuration { 

    private static $instance; 

    private $arrConfig; 

    private function __construct($configFile) 
    { 
     $this->arrConfig = parse_ini_file($configFile, true); 
     if ($this->arrConfig == false){ 
      throw new ResourceNotAvailableException("Config file {$configFile} not available."); 
     } 
    } 

    public static function getInstance($configFile = "config/config.ini") 
    { 
     if (self::$instance == null){ 
      try { 
       self::$instance = new Configuration($configFile); 
      } 
      catch(ResourceNotAvailableException $e) { 
       throw $e; 
      } 
     } 
     return self::$instance; 
    } 

} 

,這裏是我的測試類:

use Example\Lib\Configuration; 

class ConfigurationTest extends PHPUnit_Framework_TestCase { 


    /** 
    * @expectedException Example\Customexception\ResourceNotAvailableException 
    */ 
    function testGetInstanceThrowsException() 
    { 
     $configuration = Configuration::getInstance("configtestini");  
    } 


} 

這裏是輸出我得到:

PHPUnit 5.3.2 by Sebastian Bergmann and contributors. 

E                 1/1 (100%) 

Time: 89 ms, Memory: 2.75Mb 

There was 1 error: 

1) ConfigurationTest::testGetInstanceThrowsException 
parse_ini_file(configtestini): failed to open stream: No such file or directory 

/home/me/workspace/codeexample/src/Lib/Configuration.php:24 
/home/me/workspace/codeexample/src/Lib/Configuration.php:39 
/home/me/workspace/codeexample/tests/unit/Lib/ConfigurationTest.php:14 

FAILURES! 
Tests: 1, Assertions: 0, Errors: 1. 

請忽略輸出中的行號,因爲我從代碼中刪除了一些註釋,以使其在這裏易於閱讀。

感謝

回答

0

parse_ini_file()提出這是由PHPUnit的錯誤處理程序變成了一個異常的錯誤。

相關問題