【问题标题】:Can't access variables from _bootstrap.php in codeception无法在 codeception 中从 _bootstrap.php 访问变量
【发布时间】:2015-12-18 09:20:35
【问题描述】:

我使用 codeception 测试我的 laravel5 应用程序,因为我的大多数单元测试都需要一个用户,所以我尝试在我的 unit/_bootstrap.php 文件中创建一个用户:

$user = \App\User::register($credentials);

但由于我无法访问 $user 变量,因此该文件似乎没有加载到我的测试中。测试总是失败并出现以下错误:

[PHPUnit_Framework_Exception] 未定义变量:用户

我使用默认的 codeception 配置,只是添加了 laravel5 模块。我也确信引导文件会被执行,因为我可以通过在其中抛出异常来中断测试。

unit.suit.yml:

class_name: UnitTester
modules:
    enabled:
        - Asserts
        - \Helper\Unit
        - Laravel5:
            enviroment_file: .env.testing

codeception.yml

actor: Tester
coverage:
    enabled: true
    include:
        - app/Services/*
        - app/Http/*
        - app/Token.php
        - app/User.php
    exclude:
        - app/Http/routes.php
        - app/Http/Kernel.php
paths:
    tests: tests
    log: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
settings:
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
extensions:
    enabled:
        - Codeception\Extension\RunFailed
modules:
    config:
        Db:
            dsn: ''
            user: ''
            password: ''
            dump: tests/_data/dump.sql

【问题讨论】:

  • 你能分享你的套件的 Codeception 配置文件吗?如unit.suite.yml
  • 我觉得不错。它是否正在执行register() 方法的任何内容?
  • 是的,我也可以打印返回值,看看一切正常

标签: php unit-testing laravel testing codeception


【解决方案1】:

我在这里没有看到任何错误。如果你在类中使用变量$user,那么你应该添加global $user;,然后使用这个变量,因为它将是一个全局变量。

但是,使用全局变量并不是最佳解决方案。最好添加新的基类,例如 BaseCest 并在您的测试类中扩展它,然后在您的 BaseClass 中,您可以在 _before 方法中定义此类变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 2020-11-30
    • 1970-01-01
    相关资源
    最近更新 更多