mardi 29 mai 2018

Write Test On Laravel

Hello i want to write test for my Api and i create file called: LessonsTest.php

<?php

use App\Lesson;
use Tests\ApiTester;


/**
 * Class LessonsTest
*  That Test Our Lessons
*/
class LessonsTest extends ApiTester
{

/**
 * @test
 *  Test All Lessons pass
 *
 */
 public function it_fetches_lessons()
  {
  //        Make Lesson
     $this->times(5)->makeLesson();
   //      Get URL we want to test
    $this->getJson('api/v1/lessons');
   //        Pass Ok Response
    $this->assertResponseOk();


}



/**
 * Make Lesson method
 * @param array $lessonFields
 */
private function makeLesson($lessonFields = [])
{
    $lesson = array_merge([
        'title' => $this->fake->sentence,
        'body' => $this->fake->paragraph,
        'some_bool' => $this->fake->boolean
    ], $lessonFields);

    while ($this->times--) Lesson::create($lesson);
}

}

now as you see its extend from ApiTester file:

<?php

namespace Tests;


use Faker\Factory as Faker;


class ApiTester extends TestCase
{
protected $fake;

protected $times = 1;

/**
 * ApiTester constructor.
 * @param $faker
 */
public function __construct()
{
    $this->fake = Faker::create();
}

protected function times($count)
{
    $this->times = $count;
    return $this;
}


}

so its all write now problem i have when i try to test:

vendor\bin\phpunit tests\LessonsTest.php

i get this error:

1) LessonsTest::it_fetches_lessons ErrorException: array_merge(): Argument #1 is not an array

i research a lot but i could not find solution

Aucun commentaire:

Enregistrer un commentaire