1
我想写一个功能测试,以检查是否我们的REST API的方法之一正确地分派工作。编写功能测试,检查Laravel作业是否使用正确的参数进行调度?
我知道Laravel包括expectsJobs方法。但那只会检查是否派遣了这份工作。 我还需要检查它是否与实例化的PARAMS是正确的。
这样的话,我可以靠这个功能测试+,检查是否在用正确的参数实例化的作业本身运行良好另一单元测试。否则,我只知道:
- 时用正确的PARAMS
实例化,但是我不知道如果作业得到REST API调用调度工作
为了清楚起见,这里是一个位的伪代码:
为REST API调用功能测试:
$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 **/
为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 **/
正如你所看到的,有没有办法知道,如果REST API调用instantia用一些格式正确的价格来测试UpdatePricesJob。
在此先感谢!