【问题标题】:Clearing cache for laravel 4 app before codeception tests在代码接收测试之前清除 laravel 4 应用程序的缓存
【发布时间】:2015-02-11 21:47:17
【问题描述】:

我正在使用 codeception 为 laravel 应用程序运行验收测试。我遇到的一个问题是,当登录页面被缓存时,我的登录测试开始失败,我想,我正在自动登录。我认为是这种情况,因为当我清除缓存时我的测试再次开始通过,并且它们通常会开始失败,而我根本没有更改测试或应用程序代码。

这里是有问题的登录测试,现在提取到辅助方法中

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');
  }

每当测试失败时,我都会定期清除缓存,但这变得乏味。我希望能够注册一个助手来为我清除缓存并在我的所有测试中调用该函数。我按照建议here 将函数提取到一个助手中,并在 AcceptanceHelper.php 中使用以下函数:

  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

所以我被困住了。谢谢!

【问题讨论】:

    标签: php laravel-4 acceptance-testing codeception


    【解决方案1】:

    好的,所以我想出了如何做到这一点。显然有一个名为 cli 的模块,您可以将其包含在 accept.suite.yml 文件中,如下所示:

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

    此模块允许您在脚本中使用带有runShellCommand() 函数的shell 命令。由于我的缓存存储在 app/storage/cache/ 目录中的文件中,因此我需要执行的 shell 命令是:

    rm app/storage/cache/*
    

    然后清除缓存。然后在测试文件中会是这样的:

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

    我决定通过将它包含在我用来登录的函数中来简化这一点,因为我知道我想在每次登录之前清除缓存我只是将该行包含在 AcceptanceHelper 内的登录函数中,然后是在我的所有测试中都可以访问。

    这是一种解决方法,因为它与我正在使用的缓存类型无关(如果我要使用 memcached,我需要做一些不同的事情)但它对我有用,所以我想我会分享吧。

    【讨论】:

    • 您也可以使用名为 FileSystem 的模块:codeception.com/docs/modules/Filesystem 只需在您的acceptance.suite.yml 文件中启用它:modules: enabled: - Cli 然后您可以在您的测试中编写如下内容:$I->cleanDir('app/storage/cache');
    【解决方案2】:
    Artisan::call('cache:clear');
    

    是更好的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-01
      • 2021-07-30
      • 2019-01-30
      • 1970-01-01
      • 2020-08-12
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多