7
votes

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.

1
Remove other tests and show code of the whole class code (together with imports)Marcin Nabiałek
@MarcinNabiałek done!SolveSoul
There might be multiple reasons of this. Do you extend valid TestCase (you haven't included import), what is in your App model factory?Marcin Nabiałek
@MarcinNabiałek I've added the Model factory, I'm actually not importing TestCase since tests are not within a namespace, this was by default when I created the project with the Laravel command. TestCase and the unit tests are in the same directory.SolveSoul
Is this the complete error message? I can't see anything problematic here, but maybe the error is somewhere else? (In controller for example). Just to notice I don't see you are using here DatabaseMiigrations or DatabaseTransactions traitsMarcin Nabiałek

1 Answers

6
votes

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.