Livewire: When running a test using ->set() ErrorException : Unresolvable dependency resolving is thrown

Created on 28 Mar 2020  路  3Comments  路  Source: livewire/livewire

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

  1. run: artisan make:model Product
  2. run: artisan make:livewire Calculator
  3. Add a public property $product and a mount() method in the Livewire Calculator component:
 public $product;

    public function mount( $product )
    {
        $this->product = $product;
    }
  1. Create a test:
    /** @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
    }
  1. See the 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)
  2. Uncomment the // <-- 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.

Most helpful comment

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.

All 3 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tanthammar picture tanthammar  路  3Comments

pmartelletti picture pmartelletti  路  3Comments

devscript-abdo picture devscript-abdo  路  3Comments

ChrisJohnson-83 picture ChrisJohnson-83  路  4Comments

austenc picture austenc  路  3Comments