gpt4 book ai didi

使用 sqlite 的 Laravel 测试不会创建用户表

转载 作者:行者123 更新时间:2023-11-28 20:38:08 24 4
gpt4 key购买 nike

我在 Laravel 中遇到测试问题。我不知道为什么没有创建用户表,因为其他所有表都可以。

测试用的数据库是运行在内存中的Sqlite

我的 PHP 单元配置文件 (phpunit.xml)

      <php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>

用户迁移(create_users_table)

      use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user', function (Blueprint $table) {

$table->increments('id');
$table->integer('status')->nullable()->default(1);
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('sysid', 20)->nullable();
$table->string('avatar')->nullable();
$table->string('cpf', 18)->nullable();

$table->rememberToken();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user');
}
}

测试/单元/RoleTest.php

    <?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class RoleTest extends TestCase
{

use DatabaseMigrations;

protected $role;
protected $permission;


public function setUp() {

parent::setUp();
$this->role = factory('App\Model\ACL\Role')->create();
$this->permission = factory('App\Model\ACL\Permission')->create();
}


/** @test */
public function a_role_has_permissions ()
{

$this->assertInstanceOf(
'Illuminate\Database\Eloquent\Collection', $this->role->permissions
);
}

/** @test */
public function a_role_gives_permission_to ()
{
$permission = $this->role->givePermissionTo($this->permission);
$this->assertInstanceOf('App\Model\ACL\Permission', $permission);

}


/** @test */
public function a_role_has_permission ()
{
$permission = $this->role->givePermissionTo($this->permission);
$this->assertTrue($this->role->hasPermission($this->permission));
}


/** @test */
public function a_role_has_many_users ()
{

$this->assertInstanceOf(
'Illuminate\Database\Eloquent\Collection', $this->role->users
);

}
}

测试给出以下错误:

    1) Tests\Unit\RoleTest::a_role_has_many_users
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: users (SQL: select "users".*, "role_user"."role_id" as "pivot_role_id", "role_user"."user_id" as "pivot_user_id" from "users" inner join "role_user" on "users"."id" = "role_user"."user_id" where "role_user"."role_id" = 1)


Caused by
PDOException: SQLSTATE[HY000]: General error: 1 no such table: users

最佳答案

出于某种我不知道为什么的原因,我错过了迁移中的“s”,create_users_table..

但是我是如何在代码中丢失's'的:O哈哈!!!!

是一个伪错误

关于使用 sqlite 的 Laravel 测试不会创建用户表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45959245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com