Writing functional tests that check if Laravel jobs were dispatched with correct parameters?

771 Views Asked by At

I'm trying to write a functional test to check if one of the methods of our REST api correctly dispatches a job.

I know Laravel includes the expectsJobs method. But that only checks if the job was dispatched. I also need to check if the params it was instantiated with are correct.

That way, I could rely on this functional test + another unit test that checks if the job itself runs fine when instantiated with the correct parameters. Otherwise, I only know that:

  • the rest api call dispatches the job
  • the job works fine when instantiated with the correct params

But I don't know if the job gets instantiated with the correct params by the rest api call.

For clarity's sake, here is a bit of pseudo code:

Functional test for REST api call:

    $this->expectsJobs(\App\Jobs\UpdatePricesJob);
    $this->json("POST", "/api/resource/{$someresource->id}", [
        "update_prices" => 1,
        "prices" => [
          /** the prices list **/
        ]
    ])->seeJson(["success" => true]);
    /** The test succeeds if an UpdatePricesJob is dispatched **/

Unit test for UpdatePricesJob:

    $someresource = Resource::find($someResourceId);
    $newPrices = [ /** properly-formatted prices **/ ]
    $this->dispatch(new UpdatePricesJob($someresource, $newPrices));
    $this->assertEquals($someresource->prices, $newPrices);
    /** The test succeeds if running UpdatePricesJob properly updates the resource prices to the ones specified in the job params **/

As you can see, there is no way to know if the REST api call instantiates UpdatePricesJob with some properly formatted prices.

Thanks in advance!



0

There are 0 best solutions below