Laravel Unit Test: Populating model via Faker: Error- array_merge() #1 is not an array
ErrorException: array_merge(): Argument #1 is not an array
LessonsTest.php
<?php
namespace Tests\Unit;
use Tests\ApiTester;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Model\Lesson;
class LessonsTest extends ApiTester
{
use WithFaker;
public function it_fetches_lessons()
{
$this->times(5)->makeLesson();
$this->getJson('api/v1/lessons');
$this->assertOk();
}
public function makeLesson($lessonFields = [])
{
$lesson = array_merge([
'title' => $this->faker->sentence,
'body' => $this->faker->text,
'some_bool' => $this->faker->boolean
],$lessonFields);
while($this->times--) Lesson::create($lesson);
}
}
ApiTester Class
<?php
namespace Tests;
use Faker\Factory as Faker;
class ApiTester extends TestCase
{
protected $times=1;
public function times( $count)
{
$this->times = $count;
return $this;
}
}
Uniit test Result
1) Tests\Unit\LessonsTest::it_fetches_lessons
ErrorException: array_merge(): Argument #1 is not an array
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Can you share the implementation of the times function? Because I assume the ApiTester is your own class and there you have the times function. Trying without the times function on my own project, everything worked, and I don't get the array_merge error.
This does not return an error, I tried it, so either the error is someplace else in your code or debug it step by step and make sure that you enter this function.