Laravel 5 Unit Test - Call to a member function connection() on null(Laravel 5 单元测试 - 在 null 上调用成员函数 connection())
问题描述
我尝试为我的 User
和 Shop
模型之间的关系创建单元测试,但是当我运行 vendor\bin\phpunit
抛出此错误,我对此一无所知,因为我是单元测试的新手.我试图在我的控制器上运行我的代码以查看关系是否真的有效,幸运的是它按预期工作,但在 phpunit 中运行时却没有.我做错了什么让这个 phpunit 不能与模型一起工作?
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?
致命错误:
未捕获错误:在 E:projects ryvendorlaravel 中调用一个成员函数 connection() on nullframeworksrcIlluminateDatabaseEloquentModel.php:1013
堆栈跟踪:
E:projects ryvendorlaravelframeworksrcIlluminateDatabaseEloquentModel.php(979): IlluminateDatabaseEloquentModel::resolveConnection(NULL)
这是我的 UserTest.php
This is my UserTest.php
<?php
namespace TestsUnit;
use TestsTestCase;
use IlluminateFoundationTestingDatabaseMigrations;
use IlluminateFoundationTestingDatabaseTransactions;
use AppUser;
use AppShop;
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);
- 此代码在我的示例路由或控制器中运行时实际上有效,但在运行时抛出 errors
在 phpunit
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 IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser 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('AppShop');
}
}
Shop.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Shop extends Model
{
protected $guarded = [ 'id' ];
protected $table = "shops";
/**
* returns the Owner of this Shop
*/
public function owner()
{
return $this->belongsTo('AppUser');
}
}
任何帮助将不胜感激.谢谢!
Any help will be very much appreciated. Thanks!
推荐答案
首先,setUp()
在每次测试之前都会被调用,所以你不应该调用它来自构造函数
First of all, setUp()
is called before every test, so you shouldn't call it from within the constructor
其次,您应该在 setUp()
中调用 parent::setUp()
初始化应用实例.
Second of all, you should call the parent::setUp()
in your setUp()
to initialize the app instance.
这篇关于Laravel 5 单元测试 - 在 null 上调用成员函数 connection()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 5 单元测试 - 在 null 上调用成员函数 connection()
基础教程推荐
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01