gpt4 book ai didi

php - 拉维尔 : Use Eloquent Model in API Testing

转载 作者:搜寻专家 更新时间:2023-10-31 20:39:09 25 4
gpt4 key购买 nike

我想用真实的数据库插入和查询来测试 api,所以我尝试使用 setUpBeforeClasstearDownAfterClass 方法进行前后测试过程。

<?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 {

我是否在命名空间或环境方面犯了任何错误,甚至我不应该这样做?

最佳答案

EloquentModel 类的别名,因此您需要:

use Illuminate\Database\Eloquent\Model as Eloquent;

class User extends Eloquent {

或者简单地扩展模型而不使用内联别名

use Illuminate\Database\Eloquent\Model;

class User extends Model {

关于php - 拉维尔 : Use Eloquent Model in API Testing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27521397/

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