Laravel 5:如何测试调用API的Jobs?

时间:2018-03-05 07:24:04

标签: laravel laravel-5 phpunit

我一直在教自己如何编写测试用例,但我对如何测试Job是否调用API(并断言API已获得预期的响应)无能为力。< / p>

以下是我实验的片段......

Class SampleJob extends Job
{

    public function handle()
    {
        $request->method('post')->setUrl('/blahblah')->setBody($body);
        //For the sake of convenience, let me just state that
        //$request calls an API call.
        //i.e. If it's successful, you'll get
        //HTTP status 200 and a JSON object
    }
 }


Class SampleJobTest extends TestCase
{
    use DispatchesJobs;

    /** @test */
    public function it_calls_api()
    {
        $data = factory(MockData::class)->create();
        $this->dispatch(new SampleJob($data));

        //assert that the API was called

        //assert that there was an HTTP response - status & JSON
    }
}

正如评论所提到的那样,是否有可能断言API是用预期的响应调用的?

任何建议都将受到赞赏。

修改

调度SampleJob时,将调用API。

1 个答案:

答案 0 :(得分:1)

测试Json API作业有点棘手,因为你必须测试它是否是队列,调度,然后你才能知道你是否要接收响应以及它是否是预期的响应。

保持简单和有用;我将这些函数分成3个测试(调度,队列和结果),这样你就可以测试每个进程,你可以进一步扩展来测试

队列: Queue

  1. 哪个队列是推动的工作?
  2. 多少次?
  3. 发送或阻止?
  4. 公共汽车: Bus command

    1. 发送或阻止?
    2. 如果您对同一页面感兴趣,可以在队列测试中找到更多reference here,对于命令总线也可以找到相同的内容。

      这是测试类:

      <?php
      
      namespace Tests\Unit;
      
      use Tests\TestCase;
      use Illuminate\Support\Facades\Queue; // includes the fake method
      use Illuminate\Support\Facades\Bus;  // includes the fake method
      use Illuminate\Foundation\Testing\RefreshDatabase;
      use Illuminate\Foundation\Bus\DispatchesJobs; // for the Queue
      use Illuminate\Foundation\Bus\Dispatcher; // for the Bus
      use App\Jobs\APIjob as Job; // your job goes here
      
      
      class ExampleTest extends TestCase
      {
          use DispatchesJobs;
          /**
           * Setup the test environment. //  to  make the environment as a usual Laravel application which includes the helpers functions.
           *
           * @return void
           */
          protected function setup(){
              parent::setUp();
          }
      
          /**
           * A basic dispatch example.
           * 
           * @return void
           * @test
           */
          public function it_dispatches(){
              Bus::fake(); // faking the Bus command
              $job = new Job;
              Bus::dispatch($job);
              Bus::assertDispatched(Job::class, 1);
          }
      
          /**
           * A basic queue example.
           * 
           * @return void
           * @test
           */
          public function it_queues(){
              Queue::fake(); // faking Queue using the facade
              $job = new Job;
              Queue::push($job); // manually pushing the job to the Queue
              $this->dispatch($job);
              Queue::assertPushed(Job::class, 1);
          }
      
          /**
           * A basic receive example.
           *
           * @return void
           * @test
           */
          public function it_recieves_api(){
              $response =  $this->get('/APIroute'); // change this to match the route which you will receive the Json API from.
      
              $response->assertStatus(200) //
                       ->assertJsonFragment([ // using Fragment if partial, you can remove the word Fragment for full match
                              [
                              'id' => 1,
                              "name" => "Emely Jones",
                              "email" => "sgleichner@example.com",
                              "created_at" => "2018-03-05 16:36:14",
                              "updated_at" => "2018-03-05 16:36:14",
                              ],
                          ]);
          }
      }