2015-11-06 148 views
0

我剛開始學習PHPUnit。我有一個看似非常簡單的測試;PHPUnit_Framework_Assert :: assertClassHasStaticAttribute()必須是類名

namespace stats\Test; 

use stats\Fetch; 

class FetchTest extends \PHPUnit_Framework_TestCase 
{ 

    public function setUp() 
    { 
     $this->fetch = new Fetch; 
    } 

    public function testStoresListOfAssets() 
    { 
     $this->assertClassHasStaticAttribute('paths', 'Fetch'); //line 17 
    } 

} 

My Fetch class is;

namespace stats; 

class Fetch 
{ 
    public static $paths = array(
     'jquery' => 'http://code.jquery.com/jquery.js' 
    ); 
} 

運行PHPUnit時出現錯誤; PHPUnit_Framework_Exception: Argument #2 (string#Fetch)of PHPUnit_Framework_Assert::assertClassHasStaticAttribute() must be a class name

這也可能是一件很愚蠢,但我不明白的問題

回答

1

的PHPUnit_Framework_Assert使用PHP的方法class_exists檢查您是否已經指示的類名是正確的(檢查this link看到完整的代碼):

if (!is_string($className) || !class_exists($className, FALSE)) { 
    throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class name'); 
} 

你有這裏的問題是方法class_exists沒有考慮到這個命令:

use stats\Fetch; 

因此,您必須指示完整路徑才能使其正常工作。在this link of stackoverflow中,您可以找到有關該問題的更多信息。您應該將斷言改變這樣的事情:

$this->assertClassHasStaticAttribute('paths', '\\stats\\Fetch');

+0

我無意中發現這個回答我自己約30秒前。我剛剛嘗試過''stats \ Fetch'',它工作。感謝您提供正確的答案 – mikelovelyuk

0

你不提供完全合格的類名和assertClassHasStaticAttribute的情況下()或你的範圍以外的任何其他方法/函數(測試)類補充類名稱的使用語句。

如果您使用PHP 5.5或更高版本(您應該;)使用Fetch::class

一般來說,你應該更喜歡的類名字符串::類如改變類名,如果你使用字符串這是接近不可能的,當現代的IDE可以幫助您與重構。

概括起來講,你比如這將是:

public function testStoresListOfAssets() 
{ 
    $this->assertClassHasStaticAttribute('paths', Fetch::class); 
} 
相關問題