I make tests for Laravel 8 / livewire 2.7.2 app with file tests/Feature/AdminFacilitiesCrud.php :
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Livewire\Livewire;
use App\Models\User;
use App\Models\Facility;
use Livewire\Component;
use Illuminate\Database\QueryException;
//use App\Models\Settings;
//use App\Library\CheckValueType;
//use Livewire\WithPagination;
use App\Http\Livewire\Admin\CrudFacilities;
use DB;
class AdminFacilitiesCrud extends TestCase
{
// Adding the property to the params array of the Livewire::test method solved it.
// public $fingerprint= ''; // uncommenting this does not help
/** @test */
// ./vendor/bin/phpunit tests/Feature/AdminFacilitiesCrud.php
public function can_create_facility_under_admin_crud()
{
$newUser= User::factory()->create();
$this->actingAs($newUser);
Livewire::test(CrudFacilities::class)
->set('name', 'New Facility name ')
->set('descr', 'New Facility description text... ')
// ->set('fingerprint', 'fingerprint value') // uncommenting this does not help
->call('store');
$this->assertTrue(Facility::getByName('foo')->exists());
}
}
But I got error running test :
master@master-laptop:/mnt/_work_sdb8/wwwroot/lar/hostels4j$ ./vendor/bin/phpunit tests/Feature/AdminFacilitiesCrud.php
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.
Runtime: PHP 7.4.25
Configuration: /mnt/_work_sdb8/wwwroot/lar/hostels4j/phpunit.xml
E 1 / 1 (100%)
Time: 00:00.269, Memory: 28.00 MB
There was 1 error:
1) Tests\Feature\AdminFacilitiesCrud::can_create_facility_under_admin_crud
ErrorException: Undefined index: fingerprint
/mnt/_work_sdb8/wwwroot/lar/hostels4j/vendor/livewire/livewire/src/Testing/TestableLivewire.php:179
/mnt/_work_sdb8/wwwroot/lar/hostels4j/vendor/livewire/livewire/src/Testing/Concerns/MakesCallsToComponent.php:142
/mnt/_work_sdb8/wwwroot/lar/hostels4j/vendor/livewire/livewire/src/Testing/Concerns/MakesCallsToComponent.php:87
/mnt/_work_sdb8/wwwroot/lar/hostels4j/vendor/livewire/livewire/src/Testing/Concerns/MakesCallsToComponent.php:74
/mnt/_work_sdb8/wwwroot/lar/hostels4j/vendor/livewire/livewire/src/Testing/Concerns/MakesCallsToComponent.php:56
/mnt/_work_sdb8/wwwroot/lar/hostels4j/tests/Feature/AdminFacilitiesCrud.php:33
Searching in net I tried some ways to fix it, like declaring fingerprint var in test file above
or clearing cache :
composer update livewire/livewire
php artisan view:clear
php artisan livewire:publish --assets
In component app/Http/Livewire/Admin/CrudFacilities.phpI have :
<?php
namespace App\Http\Livewire\Admin;
use App\Models\Facility;
use Livewire\Component;
use Illuminate\Database\QueryException;
use App\Models\Settings;
use App\Library\CheckValueType;
use Livewire\WithPagination;
use DB;
class CrudFacilities extends Component
{
use WithPagination;
public $form= [
'name'=>'',
'descr'=> '',
'created_at'=> '',
'is_reopen' => false,
];
public $current_facility_id;
public $filter_name= '';
public $filter_extended_search= '';
public $updateMode = 'browse';
protected $listeners = ['openFacilityInEditor' => 'edit', 'addNewItem' => 'add'];
public function render()
{
...
public function store()
{
// I do not see logging lines below - so it is not store method error
\Log::info( '-1 store ::' . print_r( -1, true ) );
$this->validate( Facility::getFacilityValidationRulesArray($this->current_facility_id), Facility::getValidationMessagesArray() );
\Log::info( '-2 store $this->form ::' . print_r( $this->form, true ) );
How this error can be fixed?
Thanks !