我想使用真实的数据库插入和查询来测试api,因此我尝试在测试过程之前和之后使用setUpBeforeClass和tearDownAfterClass方法.
<?php
use App\Models\User;
class UserTest extends TestCase {
public static function setUpBeforeClass()
{
User::create([ 'name' => 'Tom' ]);
}
public function testStore()
{
$this->call('POST', 'users', [
'name' => 'Tim'
]);
$this->assertResponseOk();
}
public static function tearDownAfterClass()
{
$users = User::where('name', '=', 'Tom')
->orWhere('name', '=', 'Tim')->get();
$users->each(function($user) {
$user->delete();
});
}
}
但是,何时才能找到Eloquent却找不到.
Fatal error: Class 'Eloquent' not found in /Applications/MAMP/htdocs/edu-api/apis/app/models/User.php on line 6
<?php namespace App\Models;
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Eloquent;
class User extends Eloquent {
我在命名空间或环境上是否有任何错误,或者甚至我不应该这样做?
解决方法:
口才是Model类的别名,因此您需要:
use Illuminate\Database\Eloquent\Model as Eloquent;
class User extends Eloquent {
或简单地扩展模型而无需内联别名
use Illuminate\Database\Eloquent\Model;
class User extends Model {