简体   繁体   English

Laravel单元测试依赖注入

[英]Laravel Unit Testing Dependency Injection

I'm trying to write a test class for a shopping cart. 我正在尝试为购物车编写测试类。 Here is what I have: 这是我有的:

ShoppingCartTest.php ShoppingCartTest.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 post as well but still didn't have any luck. 我也提到了这篇文章 ,但仍然没有任何运气。

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 @edcs for guiding me in the right direction. 感谢@edcs指导我正确的方向。 You need to use a setUp function and not __construct as the app instance hasn't been created yet. 您需要使用setUp函数而不是__construct因为尚未创建app实例。

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. 但是,正确的方法是使用setUpBeforeClass()方法setUpBeforeClass()如果要执行一次初始化代码)或setUp()如果要在类中包含的每个测试之前执行初始化代码)。 Check PHPUnit documentation for more details. 查看PHPUnit文档以获取更多详细信息。

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

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