I call professions in a method:
public $professions;
public function mount()
{
$this->professions = Profession::get();
}
public function submit()
{
...
}
I display professions in a view.
<div class="form-group">
<select wire:model="profession_id" class="form-control">
@foreach ($professions as $profession)
<option value="{{$profession->id}}">{{$profession->title}}</option>
@endforeach
</select>
</div>
It works. But I write through tests. And the tests don’t work.
$data = [
'name' => 'Test',
'email' => '[email protected]',
'profession_id' => Profession::ID__ACTOR,
];
Livewire::test(SubscriberCreate::class)
->set($data) // has no $professions data
->call('submit')
->assertHasNoErrors();
I get error:
ErrorException: Invalid argument supplied for foreach()
How to solve this problem? It turns out that the mount method does not set the variable of professions in the test. I don’t want to set the variable $professions in the test. And I don’t want to check is_null($professions) in the template. It won’t be right. I want to test the work of the mount method too.
Are you using the latest version of Livewire:
latest