Laravel 5 Unit Test - Call to a member function connection() on null

前端 未结 7 1259
灰色年华
灰色年华 2020-12-31 06:53

I tried creating a unit test for the relationships between my User and Shop models, however when I run vendor\\\\bin\\\\phpunit this e

相关标签:
7条回答
  • 2020-12-31 07:18

    First of all, setUp() is called before every test, so you shouldn't call it from within the constructor

    Second of all, you should call the parent::setUp() in your setUp() to initialize the app instance.

    0 讨论(0)
  • 2020-12-31 07:28

    Cause of the problem: you can't use Laravel Models functionality from code called in the constructor of Class UserTest - even though you put the code in the method "setUp", you unnecessarily called it from the constructor. SetUp is called by phpUnit without you needing to do it in the constructor.

    When the UserTest constructor has run, the Laravel Bootstrap code has not yet been called.

    When the UserTest->setUp() method is called, the Laravel Bootstrap code HAS been run, so you can use you Models etc.

    class UserTest extends TestCase
    {protected $user, $shop;
    
    function __construct()
    {
        $this->setUp(); // **THIS IS THE PROBLEM LINE**
    }
    
    function setUp()
    {
        $user = new User([....
    
    0 讨论(0)
  • 2020-12-31 07:37

    Example:

    <?php
    
    class ExampleTest extends TestCase
    {
      private $user;
    
      public function setUp()
      {
        parent::setUp();
    
        $this->user = \App\User::first();
      }
    
       public function testExample()
       {
          $this->assertEquals('victor@castrocontreras.com', $this->user->email);
       }
    }
    
    0 讨论(0)
  • 2020-12-31 07:38

    One more reason

    check if the test class is extending use Tests\TestCase; rather then use PHPUnit\Framework\TestCase;

    Laravel ships with the later, but Tests\TestCase class take care of setting up the application, otherwise models wont be able to communicate with the database if they are extending PHPunit\Framework\TestCase.

    0 讨论(0)
  • 2020-12-31 07:39

    That can be happens when you try to read database from dataprovider function. Like me tried, yes. The work way:

    protected static $tariffs_default;
    protected function setUp(): void {
        parent::setUp ();
        if (!static::$tariffs_default){
            static::$tariffs_default = DB::...;
        }
    }    
    // in dataprovider we use string parm, named as our static vars
    public static function provider_prov2(){
        return [
            [".....", [ 'tariffs'=>'tariffs_default'] ],
        ];
    }
    // then, in test we can ask our data by code:
    public function testSome(string $inn, string $ogrn, $resp_body){
        if ( $ta_var = Arr::get( $resp_body, 'tariffs' ) ){
            Arr::set($resp_body, 'tariffs', static::$$ta_var );
        }
        $this->assert...
    }
    
    0 讨论(0)
  • 2020-12-31 07:40

    You are assigning the id '2' to both shops.

    Assigning is should not be neccessary since I assume the shop table id field is an autoincrement field.

    Also look into database factories in the Laravel docs, it will simplify things for you.

    0 讨论(0)
提交回复
热议问题