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.