Laravel 5.2 Module testing error: BadMethodCallException: Call undefined method Illuminate \ Database \ Query \ Builder :: make ()

I am trying to setup PHPunit with Laravel 5.2. I followed the documentation for a simple unit test, however each test throws the same error:

1) CreateAccountTest :: testCreateUserWithInvalidEmail BadMethodCallException: Call undefined method Highlight \ Database \ Query \ Builder :: do ()

/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 /some/path/tests/UnitTests/CreateAccountTest.php:32

My unit tests all look similar to this, except that it references a different property each 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' => '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);
    }

}

      

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

      

Factory model:

$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 pointed on the stack is the following line:

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

      

It all seems very simple according to the docs and I have no idea what is going on here.

+3


source to share


1 answer


Ok, I figured out what's going on here. I have created a field protected $app;

that accidentally overrides the $ app field from the parent class TestCase

. This converted a class from a Laravel ( Illuminate\Foundation\Application

) application Illuminate/Database/Eloquent/Model

to one where the make () function does not exist. This is why the error was thrown.



So renaming the field $app

to $application

or something else fixes the problem.

+2


source







All Articles