Laravel 5.3:添加导致状态代码500的模型方法

时间:2016-11-30 17:38:15

标签: php laravel eloquent phpunit laravel-5.3

我为Laravel 5.3项目的注册表写了一堆测试用例。其中一个基本测试用例是插入伪数据并检查正确的重定向。我的测试用例代码:

/**
 * 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的投诉。但是当我在User模型中包含以下方法时:

// 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 static $snakeAttributes = false;

同样将架构修改为驼峰案例并没有解决我的问题仍然是测试失败。

2 个答案:

答案 0 :(得分:1)

你可以采用这2种不同的方式。

<强> 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。创建一个访问者

User模型中创建以下函数:

public function getIsAdminAttribute()
{
    return $this->isAdmin;
}

这将只返回数据库中isAdmin列的值。

我个人会选择第一个选项,这是一个更好的表格命名约定,并保持一切有条理。

答案 1 :(得分:0)

我会这样做: 在您的用户模型中:

    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 add_admin_to_users_table

在您的迁移中:

/** @var ClassName $object */

这应该可以正常工作。

相关问题