2014-12-13 36 views
2

我正在使用codeception爲laravel應用運行驗收測試。我遇到的一個問題是,當登錄頁面被緩存時,我的登錄測試開始失敗,我猜,我正在自動登錄。我認爲這種情況是因爲我的測試在清除緩存時開始再次傳遞,並且一般都會在沒有更改測試或應用程序代碼的情況下開始失敗。在codeception測試之前清除laravel 4應用的緩存

這裏是有問題的登錄測試,現在提取到一個輔助方法

public function login($username, $password, $I) { 
    $I->amOnPage('users/login'); 
    $I->fillField('email', $username); 
    $I->fillField('password', $password); 
    $I->click('Login'); 
    $I->amOnPage('admin/'); 
    $I->see('Welcome'); 
    } 

我一直在定期清理緩存每當試驗失敗,但它變得乏味。我希望能夠註冊一個幫助程序來爲我清除緩存,並在我所有的測試中調用該函數。我提取的功能爲幫助在AcceptanceHelper.php建議here具有以下功能:

public function clearCache($I) { 
    $cache = $this->getModule('Cache'); 
    $cache->flush(); 
    } 

這似乎是由文檔here建議什麼,但我得到的錯誤「模塊緩存不能連接的」。它看起來像我所需要的Laravel4模塊,所以我補充說,我acceptance.suite.yml文件,但沒有運氣任我收到此錯誤:

SQLSTATE[28000] [1045] Access denied for user 'stephen'@'localhost' (using password: NO) 

我想我需要在配置文件中授權的MySQL ,但這似乎也不起作用。這是我的acceptance.suite.yml文件:

class_name: AcceptanceTester 
modules: 
    enabled: 
     - PhpBrowser 
     - AcceptanceHelper 
     - Laravel4 
    config: 
     PhpBrowser: 
      url: 'http://104.131.29.69:8000/' 
     Laravel4: 
      user: 'root' 
      password: 'pAsSwOrD' 

最後我讀this答案,似乎,好像我不應該有實際的配置文件和我的助手功能應該看起來更像,這包括Laravel4:

public function clearCache($I) { 
$L = $I->getLaravel4(); 
Cache::flush(); 
} 

但我只是風得到這個錯誤,而不是:

Class 'Codeception\Module\Cache' not found 

所以我卡住了。謝謝!

回答

1
Artisan::call('cache:clear'); 

是更好的方法。

1

好吧,我想出瞭如何做到這一點。顯然,有一個名爲cli模塊,您可以在這樣的acceptance.suite.yml文件:

class_name: AcceptanceTester 
modules: 
    enabled: 
     - PhpBrowser 
     - AcceptanceHelper 
     - Cli 

這個模塊可以讓你在你的腳本中使用shell命令與runShellCommand()功能。由於我的緩存存儲的文件在app /存儲/緩存/目錄下的shell命令我需要執行是:

rm app/storage/cache/* 

,瞧緩存清除。然後在測試文件時,它看起來就像這樣:

$I->runShellCommand('rm -rf app/storage/cache/*'); 

我決定將其包含在我用來登錄功能來簡化這個有點,因爲我知道我想每次登錄前清除緩存我只在AcceptanceHelper的一個登錄函數中包含了該行,然後在我的所有測試中都可以訪問該行。因爲它對於我正在使用的那種緩存並不是不可知的(如果我使用memcached,我需要做一些不同的事情),但它對我有用,所以我認爲我是一個很好的解決方案, d分享它。

+1

您還可以使用模塊調用文件系統:http://codeception.com/docs/modules/Filesystem 只啓用它在你的acceptance.suite.yml文件: '模塊:啓用: - Cli' 然後你可以在你的測試中寫下如下代碼: '$ I-> cleanDir('app/storage/cache');'' – 2015-08-03 11:00:33