作者:流云清动_438 | 来源:互联网 | 2023-02-08 08:11
如何解决《Laravel5.2单元测试错误:BadMethodCallException:调用未定义的方法Illuminate\Database\Query\Builder::make()》经验,求助如何解决?
我正在尝试使用Laravel 5.2设置PHPunit。我按照文档进行了简单的单元测试,但是每个测试都会引发相同的错误:
1)CreateAccountTest :: testCreateUserWithInvalidEmail BadMethodCallException:调用未定义的方法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);
}
}
概要文件控制器包含相关代码:
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()
];
});
stacktrace指出的第32行是以下行:
$this->json('POST', '/profile/create', $this->body)
->assertResponseStatus(400);
根据文档,一切似乎都非常简单,我不知道这里发生了什么。