在 Laravel 5 单元测试中找不到特征
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31253706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trait not found inside Laravel 5 unit tests
提问by John Dorean
I'm writing some unit tests to test the API endpoints in my Laravel 5 application, and a lot of endpoints require user authentication. 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.
我正在编写一些单元测试来测试我的 Laravel 5 应用程序中的 API 端点,并且许多端点需要用户身份验证。我不想在每个测试中使用相同的用户帐户创建代码,而是想定义一个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.php
并UserTest.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.
据我所知,我的命名空间是正确的,应该可以找到我的特征。我一直在绕圈子,似乎无法弄清楚。
回答by user1669496
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. You'd use it like so...
此外,当您扩展时TestCase
,它并没有将逻辑放在特征中来注册用户以用于测试,而是引入了一个帮助方法来作为某个用户登录。你会像这样使用它...
$user = User::find($id);
$this->be($user);