Laravel 5 单元测试 - 在 null 上调用成员函数 connection()

2022-08-30 15:47:38

我尝试为我的和模型之间的关系创建一个单元测试,但是当我运行这个错误时,我不知道这一点,因为我是单元测试的新手。我试图在我的控制器上运行我的代码,看看这种关系是否真的有效,幸运的是,它按预期工作,但在phpunit中运行时却没有。我做错了什么,这个phpunit不能与模型一起工作?UserShopvendor\\bin\\phpunit

致命错误:未捕获错误:在 E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1013 中调用空值上的成员函数 connection()

堆栈跟踪: E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(979): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)

这是我的用户测试.php

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

use App\User;
use App\Shop;

class UserTest extends TestCase
{

    protected $user, $shop;

    function __construct()
    {
        $this->setUp();
    }
    
    function setUp()
    {
        $user = new User([
            'id' => 1,
            'first_name' => 'John',
            'last_name' => 'Doe',
            'email' => 'JohnDoe@example.com',
            'password' => 'secret',
            'facebook_id' => null,
            'type' => 'customer',
            'confirmation_code' => null,
            'newsletter_subscription' => 0,
            'last_online' => null,
            'telephone' => null,
            'mobile' => null,
            'social_security_id' => null,
            'address_1' => null,
            'address_2' => null,
            'city' => null,
            'zip_code' => null,
            'signed_agreement' => 0,
            'is_email_confirmed' => 0
        ]);

        $user = User::find(1);
        $shop = new Shop([
            'id' => 1,
            'user_id' => $user->id,
            'name' => 'PureFoods Hotdog2',
            'description' => 'Something that describes this shop',
            'url' => null,
            'currency' => 'USD'
        ]);
        $user->shops()->save($shop);

        $shop = new Shop([
            'id' => 2,
            'user_id' => $user->id,
            'name' => 'PureFoods Hotdog',
            'description' => 'Something that describes this shop',
            'url' => null,
            'currency' => 'USD'
        ]);

        $user->shops()->save($shop);

        $this->user = $user;

    }

    /** @test */
    public function a_user_has_an_id(){
        $user =  User::find(1);
        $this->assertEquals(1, $user->id);
    }

    /** @test */
    public function a_user_has_a_first_name()
    {
        $this->assertEquals("John", $this->user->first_name);
    }

    /** @test */
    public function a_user_can_own_multiple_shops()
    {
        $shops = User::find(1)->shops()->get();
        var_dump($this->shops);
        $this->assertCount(2, $shops);
    }
}

似乎,此错误是由以下代码行引起的: - 此代码在我的示例路由或控制器中运行时实际上有效,但在运行中时会抛出$user->shops()->save($shop);errorsphpunit

用户.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $guarded = [ 'id' ];
    protected $table = "users";   

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    /**
     * returns the Shops that this user owned
     */
    public function shops()
    {
        return $this->hasMany('App\Shop');
    }
}

商店.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Shop extends Model
{
    protected $guarded = [ 'id' ];
    protected $table = "shops";

    /**
     * returns the Owner of this Shop
     */
    public function owner()
    {
        return $this->belongsTo('App\User');
    }
}

任何帮助将不胜感激。谢谢!


答案 1

首先,setUp() 在每次测试之前都会被调用,所以你不应该从构造函数内部调用它。

其次,您应该在 setUp() 中调用 parent::setUp() 来初始化应用程序实例。


答案 2

另一个原因

检查测试类是否正在扩展使用 Tests\TestCase;而不是使用 PHPUnit\Framework\TestCase;

Laravel 附带了后面的,但 Tests\TestCase 类负责设置应用程序,否则如果模型正在扩展 PHPunit\Framework\TestCase,则它们将无法与数据库通信。


推荐