0

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
  }
}
4

2 回答 2

1

Which database driver are you using? SQLite3 with :memory:?

An alternative way of checking could be to use the Entities itself. E.g.:

$this->assertEquals('test title', Post::find(1)->title);

At least you could give it a try. If this also doesn't work I would try to create the entry with Post::create(...) first. If this works, it's a problem with your Resolver. Maybe you can also show the corresponding GraphQL schema to use.

于 2020-06-08T05:42:42.450 回答
0

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();
  //...
}

于 2020-06-05T17:53:40.233 回答