【发布时间】:2020-07-03 20:48:15
【问题描述】:
我有这个代码:
/** @test */
public function testBasicExample()
{
$user = User::find(1);
// $user = factory(User::class)->create();
$response = $this->actingAs(User::find(1))->json('POST', '/store/ad', [
'title' => 'Hello World',
'city' => 1,
'phone' => '666555555',
'description' => 'fasd asd as d asd as d asd as d asd as d asd as d asd as da sd asd',
'user_id' => 1
]);
$response
->assertStatus(201)
->assertJson([
'created' => true,
]);
}
不幸的是,此时我遇到了第一个问题。它看不到 users 表。
Illuminate\Database\QueryException: SQLSTATE[HY000]: 一般错误:1 没有这样的表:用户(SQL:select * from "users" where "users"."id" = 1 个限制 1)
我正在寻找如何解决我的问题,我发现我必须使用 DatabaseMigrations。所以我补充说
use Illuminate\Foundation\Testing\DatabaseMigrations;
class ExampleTest extends TestCase
{
use DatabaseMigrations;
//...
}
但是现在我有新问题了。
TypeError:参数 1 传递给 Illuminate\Foundation\Testing\TestCase::actingAs() 必须实现 interface Illuminate\Contracts\Auth\Authenticatable, null given
所以我实现了
use Illuminate\Contracts\Auth\Authenticatable;
class ExampleTest extends TestCase
{
use DatabaseMigrations;
use Authenticatable;
//...
}
它产生了新的错误:
Tests\Feature\ExampleTest 不能使用 Illuminate\Contracts\Auth\Authenticatable - 这不是一个特征
如何解决我的问题?我该如何测试呢?
@编辑
我发现了问题,但我不知道为什么它不起作用。我有这个规则来验证城市
'city' => 'required|integer|exists:cities,id'
问题是最后一条规则:exists:cities,id。我尝试了不同的 id 存在的城市,但没有任何效果。
【问题讨论】: