Laravel Livewire测试-如何正确设置值?

问题描述

我想测试我的livewire商店功能

public function store()
{
    $validated = $this->validate(
        [
            'service_id' => 'required','price' => 'required|regex:/^[0-9]{1,5}([,\.][0-9]{1,2})?$/','option_available.*' => 'boolean','option_price.*' => 'regex:/^[0-9]{1,2})?$/'
        ]
    );

    // formatting price
    $validated['price'] = (float) str_replace(',','.',$validated['price']);

    $service = $this->services->find($validated['service_id']);

    $options = collect([]);

    if ( $this->serviceOptions ) {
        foreach ($this->serviceOptions as $key => $option) {
            $options->push(
                [
                    'name' => $option['name'],'price' => ((float) str_replace(',$this->option_price[$key]))  * 100 ?? '','available' => $this->option_available[$key] ?? false
                ]
            );
        }
    }

    $this->garage->addService($service,$options,$validated['price']);
}

这是我的测试功能

    /** @test */
function user_can_edit_garage_services_information() {

    Role::create(['name' => 'Garage Owner']);

    $garage = factory(Garage::class)->create();

    // create and attach services
    $service = Service::create(
        [
            'name' => 'Reifenwechsel','slug' => 'reifenwechsel'
        ]
    );

    // create and attach service options
    $serviceOptions = collect(
        [
            'name' => 'Wuchten','available' => false,'price' => 3
        ]
    );

    $garage->addService($service,$serviceOptions,20);

    $this->assertEquals(20,$garage->services->first()->pivot->price);
    $this->assertEquals(false,$garage->services->first()->pivot->options['available']);
    $this->assertEquals(3,$garage->services->first()->pivot->options['price']);

    Livewire::test(
        GarageServiceListItem::class,[
            'garage' => $garage
        ]
    )
        ->set('service_id',$service->id )
        ->set('price',25 )
        ->set('option_available.0',true)
        ->set('option_price.0',5)
        ->call('store');

    $garage->refresh();

    $this->assertEquals(25,$garage->services->first()->pivot->price);
    $this->assertEquals(true,$garage->services->first()->pivot->options['available']);
    $this->assertEquals(5,$garage->services->first()->pivot->options['price']);
}

尝试测试此功能时出现此错误异常:

ErrorException:未定义索引:可用

我需要传递“ option_available”和“ option_price”值,但是不知道如何执行此操作。

有人知道原因吗?

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...