Laravel 5单元测试-在null上调用成员函数connection() [英] Laravel 5 Unit Test - Call to a member function connection() on null

查看:106
本文介绍了Laravel 5单元测试-在null上调用成员函数connection()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为我的UserShop模型之间的关系创建一个单元测试,但是当我运行vendor\\bin\\phpunit时,会抛出此错误,因为我是新手,所以我对此一无所知在单元测试中.我试图在控制器上运行我的代码,以查看该关系是否确实有效,所幸该关系按预期运行,但在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中运行时实际上有效,但在phpunit

中运行时却抛出errors

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() 来初始化应用程序实例.

I tried creating a unit test for the relationships between my User and Shop models, however when I run vendor\\bin\\phpunit this error(s) are thrown, I have no idea about this since I'm a newbie in unit testing. I tried to run my code on my controller to see if the relationship actually works, and fortunately it is working as expected, but not when run in phpunit. What have I done wrong for this phpunit not to work with Models?

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)

This is my 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);
    }
}

It seems, this error is caused by this line of code: $user->shops()->save($shop); - this code actually works when run in my sample routes or Controller but is throwing errors when run in phpunit

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

Any help will be very much appreciated. Thanks!

解决方案

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.

这篇关于Laravel 5单元测试-在null上调用成员函数connection()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆