gpt4 book ai didi

php - Laravel 5.2 单元测试错误:BadMethodCallException:调用未定义的方法 Illuminate\Database\Query\Builder::make()

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:40:33 26 4
gpt4 key购买 nike

我正在尝试使用 Laravel 5.2 设置 PHPunit。我按照文档进行了简单的单元测试,但是每个测试都会抛出相同的错误:

1) CreateAccountTest::testCreateUserWithInvalidEmail BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::make()

/some/path/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2405 /some/path/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1426 /some/path/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:3526 /some/path/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:504 /some/path/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:504 /some/path/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:73 /some/path/tests/UnitTests/CreateAccountTest.php:32

我的单元测试看起来都与此类似,除了它每次都断言不同的属性:

class CreateAccountTest extends TestCase
{

protected $body;
protected $app;

protected function setUp() {
parent::setUp();

$this->app = factory(\App\Models\App::class)->create([
'name' => 'Test Suite'
]);

$this->body = [
'name' => 'Doe',
'firstName' => 'John',
'login' => 'john.doe',
'email' => 'john.doe@johndoe.com',
'password' => 'test1324',
'repeatPassword' => 'test1234',
'appId' => $this->app->id
];
}

public function testCreateUserWithInvalidEmail() {
$this->body['email'] = 'this_is_not_a_valid_email_address';

$this->json('POST', '/profile/create', $this->body)
->assertResponseStatus(400);
}

}

包含相关代码的配置文件 Controller :

<?php

namespace App\Http\Controllers\Account;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Account\CreateAccountPostRequest;
use App\Models\AccessToken;
use App\Models\User;

class ProfileController extends Controller
{

public function createUser(CreateAccountPostRequest $request) {

$user = new User();
$user->firstname = $request['firstName'];
$user->name = $request['name'];
$user->email = $request['email'];
$user->password = $request['password'];
$user->save();

$accessToken = AccessToken::createAccessToken($user->id);
$accessToken->save();

return $this->sendSuccessResponse();
}
}

模型工厂:

$factory->define(App\Models\App::class, function (Faker\Generator $faker) {
$company = $faker->company;
return [
'name' => $company,
'submit_description' => $faker->sentence($nbWords = 6, $variableNbWords = true),
'subdomain' => str_replace(' ', '', $company),
'advancedFilter' => 0,
'isdeleted' => 0,
'defaultlanguage' => 'en',
'timestamp' => time()
];
});

堆栈跟踪所述的第 32 行是以下行:

 $this->json('POST', '/profile/create', $this->body)
->assertResponseStatus(400);

根据文档,这一切看起来都非常简单,我不知道这里发生了什么。

最佳答案

好吧,我明白这是怎么回事了。我创建了一个 protected $app; 字段,它意外地覆盖了父 TestCase 类中的 $app 字段。这将类从 Laravel 应用程序 (Illuminate\Foundation\Application) 转换为 Illuminate/Database/Eloquent/Model,其上不存在 make() 函数。这就是引发错误的原因。

所以基本上,将 $app 字段重命名为 $application 或任何其他名称都可以解决问题。

关于php - Laravel 5.2 单元测试错误:BadMethodCallException:调用未定义的方法 Illuminate\Database\Query\Builder::make(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43122315/

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