dourun2990 2015-09-21 15:36 采纳率: 100%
浏览 46

无法在代码中从_bootstrap.php访问变量

I test my laravel5 application using codeception and because I need a user for most of my unit tests I tried to create one in my unit/_bootstrap.php file:

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

But it seems like this file is not getting loaded into my tests since I cant access the $user variable. The test always fails with the following error:

[PHPUnit_Framework_Exception] Undefined variable: user

I use the default codeception configuration and just added the laravel5 module. Also I know for sure that the bootstrap file gets executed since I can break the tests by throwing an exception in there.

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
  • 写回答

1条回答 默认 最新

  • doutong7216 2015-09-21 17:25
    关注

    I don't see any error here. If you use variable $user inside class, you should then add global $user; and then use this variable because it will be a global variable.

    However using global variables is not a best solution. It's probably better add new base class for example BaseCest and extend it in your test classes and then in your BaseClass you can define such variable in _before method.

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入