0

Making a test for database with Laravel (8.44.0) assetion assertModelExists(), fails on an error.

Error : Call to undefined method Tests\Feature\CommuneTest::assertModelExists()

The test class looks like this

<?php

namespace Tests\Feature;

use App\Models\Commune;
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class CommuneTest extends TestCase
{
    use RefreshDatabase, InteractsWithDatabase;

    /**
     * Test can save a commune model on DB
     *
     * @return void
     * @test
     */
    public function canStoreDb()
    {
        $commune = Commune::factory()->create();
        $this->assertModelExists($commune);
    }
}

What do you think is missing?

David L
  • 703
  • 6
  • 22
  • Do not test if the factory works as expected... That was already tested by the framework. You have to test your code, not the framework. Read [How to test](https://stackoverflow.com/questions/69150653/how-to-feature-test-more-complicated-cases-on-laravel-using-phpunit/69155061#69155061) (hope it helps you understand more stuff about testing). – matiaslauriti Jan 06 '22 at 22:29

1 Answers1

2

The assertModelExists method was added September 2021:

https://github.com/laravel/framework/pull/38766

Version 8.44.0 was released May 2021:

https://github.com/laravel/framework/releases/tag/v8.44.0

So just update Laravel to the latest 8.x release and you should be good to go.

jszobody
  • 28,495
  • 6
  • 61
  • 72