在Laravel上写测试

时间:2018-05-29 15:53:20

标签: php laravel api testing phpunit

您好我想为我的Api编写测试并创建一个名为: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);
}

}

现在,当您看到它从ApiTester文件扩展时:

<?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;
}


}

所以当我尝试测试时,我现在所有写的问题:

vendor \ bin \ phpunit tests \ LessonsTest.php

我收到此错误:

1)LessonsTest :: it_fetches_lessons ErrorException:array_merge():参数#1不是数组

我研究了很多,但我找不到解决方案

0 个答案:

没有答案