只需玩laravel-8单元测试。我扩展了基本的TestCase并认为laravels工厂方法将可用。我检查了composer.json并正在加载工厂。
我正在尝试运行此特定测试,但是找不到factory
的任何想法:
<?php
namespace Tests\Feature\Http\Controllers\Auth;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\User;
class LoginControllerTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function login_authenticates_and_redirects_user()
{
$user = factory(User::class)->create();
$response = $this->post(route('login'), [
'email' => $user->email,
'password' => 'password'
]);
$response->assertRedirect(route('home'));
$this->assertAuthenticatedAs($user);
}
}
我得到的错误是:
1) Tests\Feature\Http\Controllers\Auth\LoginControllerTest::login_authenticates_and_redirects_user
Error: Call to undefined function Tests\Feature\Http\Controllers\Auth\factory()
答案 0 :(得分:0)
有关升级到laravel-8发行说明的结果:
“ Laravel的模型工厂功能已完全重写以支持类,并且与Laravel 7.x样式工厂不兼容。”
因此,为了使其正常工作,我使用了:
$user = \App\Models\User::factory(User::class)->make();
答案 1 :(得分:0)