2014-01-29 53 views
13

我正在使用Codeception進行Laravel 4 PHP應用程序的單元,功能和驗收測試。如何在Codeception功能測試中使用PHPUnit聲明方法?

我的單元測試,看看這個:

use Codeception\Util\Stub; 
class ExampleTest extends \Codeception\TestCase\Test 
{ 
public function testExample() 
{ 
    $example = true; 
    $this->assertSame($example, true); 
} 
} 

我的功能測試是這樣的:

use \TestGuy; 
class ExampleCest 
{ 
public function example(TestGuy $I) 
{ 
    $I->amOnPage('/auth/login'); 
    $I->see('Sign in'); 
} 
} 

但我也想用PHPUnit的斷言在我的功能測試方法。但是,當我嘗試,我得到這個錯誤:

調用未定義的方法ExampleCest :: assertSame()

如何使用PHP assert方法在Codeception功能測試?

回答

11

\PHPUnit_Framework_Assert::assertSame()

0

另一個解決方法可以是使用輔助方法在測試套件。

例如,對於assertSame()方法

class ExpectedHelper extends \Codeception\Module 
{ 
    protected $test; 

    function _before(\Codeception\TestCase $test) { 
     $this->test = $test; 
    } 

    function assertSame($expected, $actual, $message = '') 
    { 
     $this->test->assertSame($exception, $actual, $message); 
    } 
} 

其中ExpectedHelper被測試套件助手名稱(例如:UnitHelperFunctionalHelper),其應該是下_support

你可以在你的測試使用它作爲$I->assertSame('12340','12340');

20

由於Codeception 2.1(不是2.0)像其他斷言你可以使用它:

$I->assertSame($expected, $actual, $message); 

但是不要忘記,以使Asserts模塊在你的配置 - 例如:

class_name: UnitTester 
modules: 
    enabled: [ Asserts ] 

請注意:升級到2.1時,您可能需要更改您的配置 - 請參閱升級instru提示:http://codeception.com/06-19-2015/codeception-2.1-rc.html

+3

這裏是「如何在功能測試中使用Assert方法」問題的最佳答案。 – lintuxvi

+3

爲了在您的IDE中看到其他方法,您必須在配置更改後運行一次測試套件。來自代碼隱藏文檔:「默認情況下,Codeception會在每次更改套件配置時自動重建動作特徵。」 –

相關問題