Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 Unit Tests error: BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::make()

I'm trying to set-up PHPunit with Laravel 5.2. I followed the documentation for a simple unit test, however every test throws the same error:

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

My unit tests all look similar to this, except it's asserting a different property every time:

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' => '[email protected]',
            '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);
    }

}

Profile controller containing relevant code:

<?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();
    }
}

Model factory:

$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()
    ];
});

Line 32 as stated by the stacktrace is the following line:

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

It all seems very straightforward according to the docs and I have no idea what is happening here.

like image 658
SolveSoul Avatar asked Mar 30 '17 15:03

SolveSoul


People also ask

What is badmethodcallexception 2437 in Laravel?

BadMethodCallException in Builder.php line 2437: Call to undefined method Illuminate\Database\Query\Builder::lists() listsmethod on the collection in Laravel 5.3 has been renamed to pluck.

What is Laravel query builder?

Database: Query Builder - Laravel - The PHP Framework For Web Artisans Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things. Skip to content Prologue

What are the new features in Laravel 5 Tinker?

What are the new features in Laravel 5.5 Laravel Tinker with PHP Artisan command to update user details Laravel 5.4 User Role and Permissions with Spatie Laravel Permission Laravel 5 force download file with the response helper method Force update updated_at column using Eloquent Touch method

Why choose Laravel for your next project?

We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by easing common tasks used in most web projects. Highlights Our Team Release Notes Getting Started


1 Answers

Okay, I figured out what was going on here. I made a field protected $app; which accidentally overrides the $app field from the parent TestCase class. This converted the class from a Laravel app (Illuminate\Foundation\Application) to Illuminate/Database/Eloquent/Model on which the make() function does not exist. That's why the error was thrown.

So basically, renaming the $app field to $application or anything else resolves the problem.

like image 54
SolveSoul Avatar answered Nov 14 '22 06:11

SolveSoul