【问题标题】:How can I off dispatch() helper in Laravel controller method when testing?测试时如何在 Laravel 控制器方法中关闭 dispatch() 助手?
【发布时间】:2019-05-20 00:59:23
【问题描述】:

我在测试这种控制器方法时遇到问题:

public function foo() {
    $data = [
        'content' => $content,
        'from' => date('Y-m-d H:i:s'),
        'to' => date('Y-m-d H:i:s'),
    ];
    $report = (new BlockedItemsReport($data))->onQueue('default');
    dispatch($report);
    return back();

}

这是一种测试方法:

public function testFoo()
{
    $faker = Faker\Factory::create();
    $response = $this->call('POST', route('make.foo'), [
        'content' => $faker->text(200),
    ]);
    $response->assertRedirect('/');
}

我应该如何使用 dispatch() 助手来关闭操作?

【问题讨论】:

    标签: php laravel unit-testing phpunit


    【解决方案1】:

    这样试试,

    您的控制器

    public function foo($check) {
        $data = [
            'content' => $content,
            'from' => date('Y-m-d H:i:s'),
            'to' => date('Y-m-d H:i:s'),
        ];
        $report = (new BlockedItemsReport($data))->onQueue('default');
    
        if($check){
           dispatch($report);  Подскажите, как?
        }
        return back();
    }
    

    你的测试方法

    public function testFoo()
    {
        $faker = Faker\Factory::create();
        $response = $this->call('POST', route('make.foo',[$check => false]), [
            'content' => $faker->text(200),
        ]);
        $response->assertRedirect('/');
    }
    

    希望你平安:)

    【讨论】:

      猜你喜欢
      • 2015-06-29
      • 1970-01-01
      • 2021-05-03
      • 2011-06-07
      • 2018-12-28
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多