Laravel工厂找不到数据库表

时间:2020-03-27 10:50:32

标签: php laravel factory

我正在与Laravel 6.17.1工厂一起使用非常简单的模型进行测试,只是一个字段:'money'一个整数。这是我的工厂:

$factory->define(wallet::class, function (Faker $faker) {
    return [
        'money' => $faker->numberBetween($min = 500, $max=900)
    ];
});

但是当我使用vendor/bin/phpunit运行测试时,出现下一个错误:

一般错误:1没有这样的表格:wallets(SQL:插入“ wallets”(“ money”,“ updated_at”,“ created_at”)值(545,2020-03-27 10:20:12,2020- 03-27 10:20:12))

但是该表确实存在并且我的模型确实适合它,我通过在控制器中创建一个函数并在表中创建一些数据来对其进行检查:

public function dummyCreation(){
    wallet::create(['money' => 100]);
    return 'works!';
}

evidence of model working

有人可以帮我吗?如果有帮助,我将在下面添加我的模型和测试:

class wallet extends Model
{
    protected $fillable = ['money'];

    public function transfers(){
        return $this->hasMany('App\Transfer');
    }
}
public function testGetWallet()
{
    $wallet    = factory(Wallet::class     )->create();
    $transfers = factory(Transfer::class, 3)->create([
        'wallet_id' => $wallet->id
    ]);

    $response = $this->json('GET','/api/wallet');

    $response->assertStatus(200)
    ->assertJsonStructure([
        'id', 'money', 'transfers' =>[
            '*' =>[
                'id', 'amount', 'description', 'wallet_id'
            ]
        ]
    ]);

    $this->assertCount(3, $response->json()['transfers']);
}

0 个答案:

没有答案