Laravel 单元测试依赖注入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40657440/
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
Laravel Unit Testing Dependency Injection
提问by Denis Priebe
I'm trying to write a test class for a shopping cart. Here is what I have:
我正在尝试为购物车编写一个测试类。这是我所拥有的:
ShoppingCartTest.php
购物车测试.php
class ShoppingCartTest extends TestCase {
use DatabaseTransactions;
protected $shoppingCart;
public function __construct() {
$this->shoppingCart = resolve('App\Classes\Billing\ShoppingCart');
}
/** @test */
public function a_product_can_be_added_to_and_retrieved_from_the_shopping_cart() {
// just a placeholder at the moment
$this->assertTrue(true);
}
}
However, when I run phpunit, it seems like Laravel is unable to resolve my ShoppingCartClass.
但是,当我运行 phpunit 时,Laravel 似乎无法解析我的 ShoppingCartClass。
Here is the error:
这是错误:
Fatal error: Uncaught exception 'Illuminate\Contracts\Container\BindingResolutionException'
with message 'Unresolvable dependency resolving
[Parameter #0 [ <required> $app ]] in class Illuminate\Support\Manager'
in C:\Development Server\EasyPHP-Devserver-16.1\eds-www\nrponline\vendor\laravel\framework\src\Illuminate\Container\Container.php:850
I have my ShoppingCart class being resolved in a number of different controllers just fine.
我的 ShoppingCart 类在许多不同的控制器中都得到了很好的解决。
Why can't Laravel resolve it during my tests?
为什么 Laravel 在我的测试中不能解决它?
I refered to this postas well but still didn't have any luck.
我也参考了这篇文章,但仍然没有任何运气。
回答by Denis Priebe
I figured it out. Here is the updated class.
我想到了。这是更新的课程。
class ShoppingCartTest extends TestCase {
use DatabaseTransactions;
protected $shoppingCart;
public function setUp() {
parent::setUp();
$this->shoppingCart = $this->app->make('App\Classes\Billing\ShoppingCart');
}
/** @test */
public function a_product_can_be_added_to_and_retrieved_from_the_shopping_cart() {
// just a placeholder at the moment
$this->assertTrue(true);
}
}
Thanks to @edcsfor guiding me in the right direction.
You need to use a setUp function and not __construct
as the app
instance hasn't been created yet.
感谢@edcs引导我朝着正确的方向前进。您需要使用 setUp 函数,而不是__construct
因为app
尚未创建实例。
回答by Andrea Mauro
If you want to use __construct
you have to use the same constructor of PHPUnit\Framework\TestCase
and remember to call the parent method if you don't want to break anything
如果你想使用__construct
你必须使用相同的构造函数,PHPUnit\Framework\TestCase
如果你不想破坏任何东西,记得调用父方法
class MyTest extends TestCase
{
public function __construct($name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
// my init code
}
}
However the proper way would be to use the method setUpBeforeClass()
if you want to execute your init code once or setUp()
if you want to execute the init code before each test contained in your class.
Check PHPUnit documentationfor more details.
但是,setUpBeforeClass()
如果您想执行一次 init 代码或者setUp()
想在类中包含的每个测试之前执行 init 代码,则正确的方法是使用该方法。查看PHPUnit 文档以获取更多详细信息。