Search code examples
laravelphpunitlaravel-lighthouselaravel-testing

Testing Laravel lighthouse mutations with database


I am trying to use database and laravel-lighthouse in tests. I am using the traits for GraphQL and Database Testing, mutation works but tests don't.

class MyTest extends TestCase{
  use MakesGraphQLRequests;
  use RefreshDatabase;
  public function testGraphQLDatabaseTest(){
    $this->graphQL(/** @lang GraphQL */ '
        mutation CreatePost($title: String!) {
            createPost(title: $title) {
                id
            }
        }
    ',['title' => 'test title']);

    $this->assertDatabaseHas('posts', [
        'title' => 'test title',
    ]); // It says that expect table posts with title' => 'test title' but 'posts' table is empty
  }
}

Solution

  • I have some Facades in the mutation in this case I just add the facades fake or stub to figure out the issue.

    // Example
    // At the beginning of the test
    public function testGraphQLDatabaseTest(){
      Mail::fake();
      //...
    }