热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Laravel5.2单元测试错误:BadMethodCallException:调用未定义的方法Illuminate\Database\Query\Builder::make()

如何解决《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);

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


推荐阅读
author-avatar
流云清动_438
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有