gpt4 book ai didi

php - 无法在 codeception 中访问 _bootstrap.php 中的变量

转载 作者:行者123 更新时间:2023-11-28 20:54:08 24 4
gpt4 key购买 nike

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

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

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

[PHPUnit_Framework_Exception] Undefined variable: user

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

单位.套装.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

最佳答案

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

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

关于php - 无法在 codeception 中访问 _bootstrap.php 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32699221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com