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

标签 php unit-testing laravel-5 eloquent phpunit

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

Fatal error: Uncaught Error: Call to a member function connection() on null in E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1013

Stack trace: E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(979): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)

这是我的 UserTest.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); - 此代码在我的示例路由或 Controller 中运行时实际上有效,但抛出 errorsphpunit 中运行时

User.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');
    }
}

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');
    }
}

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

最佳答案

首先,setUp() 在每次测试之前都会被调用,因此您不应该在构造函数中调用它

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

关于php - Laravel 5 单元测试 - 在 null 上调用成员函数 connection(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42512676/

相关文章:

unit-testing - Grails test-app未执行语句

php - 截断长字符串,不打断带点插值的单词

php - laravel 路由直接进入文件夹和文件

unit-testing - 通过嵌入式脚本从应用程序内部进行测试是个好主意吗?

php - 从 Laravel 中的数据库模型中获取所有字段

php - Laravel 使用关系显示 View ,加入

php - Image.php 第 143 行中的 NotWritableException : Can't write image data to path (C:\Users\SurajLifeean\surj\public\images/)

Php -MySQL 查询失败 - 错误 fatal error : Uncaught Error: Call to a member function fetch_assoc() on boolean

php - 当超过 post_max_size 时,CakePHP 会黑洞文件上传

angular - 如何修复 Angular 测试中的 "ViewDestroyedError: Attempt to use a destroyed view"错误?