Describe the bug
Using PHP 7.4, Livewire 1.0.10 (latest), Laravel 7.x (latest)
Livewire::test( Calculator::class )
->set( 'product', $product );
Returns:
ErrorException : Unresolvable dependency resolving [Parameter #0 [ <required> $product ]] in class App\Http\Livewire\Product\Calculator (View: /opt/project/vendor/livewire/livewire/src/views/mount-component.blade.php)
However, this runs fine:
Livewire::test( Calculator::class, [ 'product' => $product ] );
To Reproduce
$product and a mount() method in the Livewire Calculator component: public $product;
public function mount( $product )
{
$this->product = $product;
}
/** @test */
public function it_can_use_the_set_function_to_set_a_product()
{
/** @var Product $product */
$product = factory( Product::class )->create();
// Livewire::test( TestCalculator::class, [ 'product' => $product ] ); // <-- works
Livewire::test( TestCalculator::class)
->set( 'product', $product ); // <-- error
}
ErrorException : Unresolvable dependency resolving [Parameter #0 [ <required> $product ]] in class App\Http\Livewire\Product\Calculator (View: /opt/project/vendor/livewire/livewire/src/views/mount-component.blade.php)// <-- works line, and see that pass.Expected behavior
To be able to use the ->set() command, just like the docs tell us, instead of the inline version.
Hello @basepack, It's because you didn't give $product and default value like null or ''.
So that why you need to give it a second parameter while calling Livewire::test(..., [...]).
The ->set() function can be handy to check if certain side effects are done when you change the value later on and make assertions on it.
I hope this made it more clear why this happened. I think this is just the only way when the mount function doesn't have default values for the variables.
Exactly what Gertjan said. Thanks @GertjanRoke !
@GertjanRoke I spent 2 hours on this tonight. Thanks for the help!
Most helpful comment
Hello @basepack, It's because you didn't give
$productand default value likenullor''.So that why you need to give it a second parameter while calling
Livewire::test(..., [...]).The
->set()function can be handy to check if certain side effects are done when you change the value later on and make assertions on it.I hope this made it more clear why this happened. I think this is just the only way when the
mountfunction doesn't have default values for the variables.