简体   繁体   English

在 Laravel 5 单元测试中找不到特征

[英]Trait not found inside Laravel 5 unit tests

I'm writing some unit tests to test the API endpoints in my Laravel 5 application, and a lot of endpoints require user authentication.我正在编写一些单元测试来测试我的 Laravel 5 应用程序中的 API 端点,并且许多端点需要用户身份验证。 Instead of having the same user account creation code in every test, I wanted to define a RegistersUsers trait to use on the test classes, which will have a registerUser() method.我不想在每个测试中使用相同的用户帐户创建代码,而是想定义一个RegistersUsers特性以在测试类上使用,该特性将具有registerUser()方法。

The directory structure of my tests directory is like so:我的tests目录的目录结构是这样的:

/tests
    /Traits
        RegistersUsers.php
    TestCase.php
    UserTest.php

I've namespaced TestCase.php and UserTest.php by adding this namespace declaration:我通过添加这个命名空间声明来命名TestCase.phpUserTest.php

namespace MyappTests;

and I've namespaced RegistersUsers.php like so:我已经命名了RegistersUsers.php像这样:

namespace MyappTests\Traits;

My UserTest looks like this, with the namespace and the use declaration so that I can leverage RegistersUsers .我的UserTest看起来像这样,带有命名空间和use声明,以便我可以利用RegistersUsers

<?php

namespace MyappTests;

use MyappTests\Traits\RegistersUsers;

class UserTest extends TestCase
{
    use RegistersUsers;

    // ... rest of the class

However, when I run the test, PHPUnit dies with the fatal error:但是,当我运行测试时,PHPUnit 因致命错误而死:

Trait 'MyappTests\\Traits\\RegistersUsers' not found in /home/vagrant/demo-app-net/tests/UserTest.php on line 9在第 9 行的 /home/vagrant/demo-app-net/tests/UserTest.php 中找不到特征“MyappTests\\Traits\\RegistersUsers”

As far as I can tell, my namespacing is correct and my trait should be found.据我所知,我的命名空间是正确的,应该可以找到我的特征。 I've been going around in circles with this and can't seem to figure it out.我一直在绕圈子,似乎无法弄清楚。

I'm guessing having the trait in the traits folder, the trait is no longer accounted for in your autoloader.我猜在traits 文件夹中有这个trait,你的自动加载器中不再考虑这个trait。

In order to correct this, you should open up composer.json , find the sectionfor autoload-dev and change it to something like the following...为了纠正这个问题,你应该打开composer.json ,找到autoload-dev并将其更改为如下所示...

"autoload-dev": {
    "classmap": [
        "tests/TestCase.php",
        "tests/Traits/"
    ]
},

And that should add any traits you have in that folder to the autloader.这应该会将您在该文件夹中拥有的任何特征添加到自动加载程序中。

Edit编辑

Some additional ideas were brought up in the comments.评论中提出了一些额外的想法。 If you are going to be maintaining proper folder/namespace structure, it would be a good idea to use psr-4 autoloading rather than maintaining the class map.如果您要维护正确的文件夹/命名空间结构,最好使用 psr-4 自动加载而不是维护类映射。

"autoload-dev": {
    "psr-4": {
        "MyappTests\\": "tests/"
    }
},

Also, rather than put logic in a trait to register a user for use with testing, when you extend TestCase , it brings in a helper method for logging in as a certain user.此外,当您扩展TestCase ,它并没有将逻辑放在特征中来注册用户以用于测试,而是引入了一个帮助方法来作为某个用户登录。 You'd use it like so...你会像这样使用它...

$user = User::find($id);
$this->be($user);

I'm developing a JSON REST API, and during tests development I found the need to refactor some of my testing functionalities inside a trait to be used inside test classes.我正在开发一个 JSON REST API,在测试开发期间,我发现需要在要在测试类中使用的 trait 中重构我的一些测试功能。

After renaming a class, PhpStorm wrote in the log something like: Fatal error: Trait 'controllers\\ControllerTestWithUsers2' not found in ... when I tried to execute my test suite using the renamed trait.重命名一个类后,PhpStorm 在日志中写道: Fatal error: Trait 'controllers\\ControllerTestWithUsers2' not found in ...当我尝试使用重命名的 trait 执行我的测试套件时。

The solution to my problem was to simply run the command composer dump-autoload and then try again to run my test suite.我的问题的解决方案是简单地运行命令composer dump-autoload然后再次尝试运行我的测试套件。

I found that this operation has to be done every time a renaming is performed in PhpStorm.我发现每次在 PhpStorm 中执行重命名时都必须执行此操作。 It seems that the composer command regenerates some internal files pointing to the old class name, leading to the "Fatal error".似乎 composer 命令重新生成了一些指向旧类名的内部文件,导致了“致命错误”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM