Php Laravel 5.3:添加导致状态代码500的模型方法
我已经为Laravel5.3项目的注册表编写了一系列测试用例。基本测试用例之一是插入假数据并检查是否正确重定向。我的测试用例代码:Php Laravel 5.3:添加导致状态代码500的模型方法,php,laravel,eloquent,phpunit,laravel-5.3,Php,Laravel,Eloquent,Phpunit,Laravel 5.3,我已经为Laravel5.3项目的注册表编写了一系列测试用例。基本测试用例之一是插入假数据并检查是否正确重定向。我的测试用例代码: /** * Check for the registration form * * @return void */ public function testNewUserRegister(){ // Generate a ranom name $thisName = str_random(8); // Genearte a random emai
/**
* Check for the registration form
*
* @return void
*/
public function testNewUserRegister(){
// Generate a ranom name
$thisName = str_random(8);
// Genearte a random email
$thisEmail = str_random(8)."@google.com";
// Type some valid values
$this->visit('/register')
->type($thisName,'name')
->type($thisEmail,'email')
->type('password123','password')
->type('password123','password_confirmation')
->press('Register')
->seePageIs('/home');
}
一切正常,没有来自PHPUnit
的投诉。但是当我在我的用户
模型中包含以下方法时:
// Function to check for the adminship
public function isAdmin(){
// Check if the user is logged in
if(Auth::check()){
// Check if the user is admin
if(Auth::user()->isAdmin){
return true;
}
}
// Return false if not
return false;
}
上述测试用例testNewUserRegister
失败,错误消息如下:
There was 1 failure:
1) AuthTest::testNewUserRegister
A request to [http://localhost/home] failed. Received status code [500].
/var/www/html/project-css/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:220
/var/www/html/project-css/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:92
/var/www/html/project-css/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:150
/var/www/html/project-css/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:92
/var/www/html/project-css/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:125
/var/www/html/project-css/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:580
/var/www/html/project-css/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:567
/var/www/html/project-css/tests/AuthTest.php:26
Caused by
ErrorException: Undefined property: App\User::$isAdmin in /var/www/html/project-css/app/User.php:36
此外,以下是数据库模式:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->boolean('isAdmin')->default(false);
$table->boolean('hasAccess')->default(true);
$table->rememberToken();
$table->timestamps();
});
原因如下:
public function isAdmin(){
// this looks for an admin column in users table
return $this->admin;
}
public function up()
{
Schema::table('users', function ($table) {
$table->boolean('admin')->default(0);
});
}
public function down(){
Schema::table('users', function ($table) {
$table->dropColumn('admin');
});
这并不是因为骆驼案。当包含以下参数时,测试仍然失败:
public static $snakeAttributes = false;
另外,将模式修改为驼峰案例并没有解决我的问题,但测试仍然失败。您可以用两种不同的方法进行测试 1。更改迁移
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->boolean('is_admin')->default(false);
$table->boolean('has_access')->default(true);
$table->rememberToken();
$table->timestamps();
});
2。创建访问器
在用户
模型中创建以下函数:
public function getIsAdminAttribute()
{
return $this->isAdmin;
}
这将只返回数据库中isAdmin
列的值
我个人会选择第一个选项,这是一个更好的表命名约定,可以使所有内容都井然有序。我会这样做:
在您的用户模型中:
public function isAdmin(){
// this looks for an admin column in users table
return $this->admin;
}
public function up()
{
Schema::table('users', function ($table) {
$table->boolean('admin')->default(0);
});
}
public function down(){
Schema::table('users', function ($table) {
$table->dropColumn('admin');
});
现在向数据库添加一个管理字段:
public function isAdmin(){
// this looks for an admin column in users table
return $this->admin;
}
public function up()
{
Schema::table('users', function ($table) {
$table->boolean('admin')->default(0);
});
}
public function down(){
Schema::table('users', function ($table) {
$table->dropColumn('admin');
});
php artisan将_admin_添加到_users_表
在您的迁移中:
public function isAdmin(){
// this looks for an admin column in users table
return $this->admin;
}
public function up()
{
Schema::table('users', function ($table) {
$table->boolean('admin')->default(0);
});
}
public function down(){
Schema::table('users', function ($table) {
$table->dropColumn('admin');
});
这应该可以正常工作。告诉我比这更清楚的是什么:
ErrorException:Undefined属性:App\User::$isAdmin in/var/www/html/project css/App/User.php:36
@Kyslik架构中有一个属性isAdmin
。@PseudoAj你能给我们看一下你的架构吗?可能是@ceejayoz的副本,你有没有尝试过吗将架构更改为驼峰案例无效。我的迁移中已经有了管理员$this
无法解决此问题。您已经在您的用户模型中,因此不需要添加check()
方法。我在这里给你的,做得很好,我的朋友同意。但我相信并证实问题不在于检查方法。它使用了isAdmin
属性。您是否尝试像我上面所写的那样执行此操作?如果没有,那么你会看到它的工作。如果您有admin字段,那么不要添加它,只需将您的isAdmin函数替换为my函数即可。那你就可以正确使用了。