User
とShop
モデルの間の関係のユニットテストを作成しようとしましたが、vendor\\bin\\phpunit
を実行するとこのエラーがスローされます。私は単体テストの初心者ですから、これについてのアイデアです。私は自分のコントローラ上で自分のコードを実行して関係が実際に動作するかどうかを調べようとしましたが、幸いにも期待どおりに動作していますが、phpunitで動作していません。私は、このphpunitがModelsと連携しないために間違っていたことは何ですか?Laravel 5.4ユニットテスト - ヌルのメンバー関数connection()への呼び出し
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) E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(843): Illuminate\Database\Eloquent\Model->getConnection() E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(804): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder() E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(788): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes() E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1316): Illuminate\Database\Eloquent\Model->newQuery() E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1328): Illum in E:\projects\try\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 1013
これは、それはそうです、私の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);
- このコードは実際に私のサンプルのルートやコントローラで実行したときに動作しますが、あります
phpunit
で実行した場合errors
を投げます
<?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');
}
}
すべてのヘルプは非常に高く評価されます。ありがとう!