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

前端 未结 7 1264
灰色年华
灰色年华 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: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([....
    

提交回复
热议问题