Php 编写功能测试,检查是否使用正确的参数分派了Laravel作业?
我正在尝试编写一个功能测试,以检查RESTAPI的一个方法是否正确地分派了一个作业 我知道Laravel包括expectsJobs方法。但这只会检查作业是否已分派我还需要检查实例化时使用的参数是否正确。 这样,我就可以依靠这个功能测试+另一个单元测试来检查当使用正确的参数实例化时作业本身是否运行良好。否则,我只知道:Php 编写功能测试,检查是否使用正确的参数分派了Laravel作业?,php,laravel,unit-testing,phpunit,functional-testing,Php,Laravel,Unit Testing,Phpunit,Functional Testing,我正在尝试编写一个功能测试,以检查RESTAPI的一个方法是否正确地分派了一个作业 我知道Laravel包括expectsJobs方法。但这只会检查作业是否已分派我还需要检查实例化时使用的参数是否正确。 这样,我就可以依靠这个功能测试+另一个单元测试来检查当使用正确的参数实例化时作业本身是否运行良好。否则,我只知道: RESTAPI调用分派作业 当使用正确的参数实例化作业时,该作业工作正常 但我不知道RESTAPI调用是否使用正确的参数实例化了作业 为了清楚起见,这里有一些伪代码: REST
- RESTAPI调用分派作业
- 当使用正确的参数实例化作业时,该作业工作正常
$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 **/
$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 **/
更新许可证作业的单元测试:
$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 **/
$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调用是否使用一些格式正确的价格实例化了UpdatePricesJob
提前谢谢