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

问题描述 投票:0回答:9

我尝试为我的

User
Shop
模型之间的关系创建一个单元测试,但是当我运行
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' => '[email protected]',
            '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);
- 此代码在我的示例路由或控制器中运行时实际上有效,但在
errors
 中运行时抛出 
phpunit

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

任何帮助将非常感激。谢谢!

php unit-testing laravel-5 eloquent phpunit
9个回答
54
投票

还有一个原因

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

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


48
投票

首先,

setUp()
在每次测试之前都会被调用,所以你不应该在构造函数中调用它

其次,您应该在

parent::setUp()
中调用
setUp()
来初始化应用程序实例。


14
投票

示例:

<?php

class ExampleTest extends TestCase
{
  private $user;

  public function setUp()
  {
    parent::setUp();

    $this->user = \App\User::first();
  }

   public function testExample()
   {
      $this->assertEquals('[email protected]', $this->user->email);
   }
}

13
投票

解决方案

使用PHPunit\Framework\TestCase

放这个

使用测试\测试用例


1
投票

您正在为两家商店分配 ID“2”。

分配 is 应该不是必需的,因为我假设商店表 id 字段是自动增量字段。

另请参阅 Laravel 文档中的数据库工厂,它会为您简化事情。


1
投票

问题原因:您无法从 UserTest 类的构造函数中调用的代码中使用 Laravel Models 功能 - 即使您将代码放在“setUp”方法中,您也不必要从构造函数中调用它。 SetUp 由 phpUnit 调用,无需在构造函数中执行。

当 UserTest 构造函数运行时,Laravel Bootstrap 代码尚未被调用。

当调用 UserTest->setUp() 方法时,Laravel Bootstrap 代码已运行,因此您可以使用模型等。

class UserTest extends TestCase
{protected $user, $shop;

function __construct()
{
    $this->setUp(); // **THIS IS THE PROBLEM LINE**
}

function setUp()
{
    $user = new User([....

1
投票

当您尝试从 dataprovider 函数读取数据库时,可能会发生这种情况。就像我尝试过的那样,是的。工作方式:

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
投票

尝试

composer dump-autoload

~问候


0
投票

在 PEST PHP 中使用这个:

<?php

use Tests\TestCase;

uses(TestCase::class);
...
© www.soinside.com 2019 - 2024. All rights reserved.