1

I have a trait that records activity on the created lifecycle hook. In my test class, if I run any given single test with RefreshDatabase it fails saying the table is empty. If I run the test without RefreshDatabase it passes but then fails the next time for obvious reasons. If I run the entire test class with RefreshDatabase only the first one fails and the rest pass. If I run the entire test class without RefreshDatabase the first one passes and the rest fail because of obvious reasons (duplicate key errors).

Here is one of the tests behaving this way:

    /** @test */
    public function user_can_access_their_own_activity()
    {
        $this->jsonAs($this->user, 'POST', route('team.store'), [
            'display_name' => 'Test Team',
        ])->assertStatus(200);

        $this->assertDatabaseHas('activities', [
            'user_id' => $this->user->getKey(),
            'type' => 'created_team',
        ]);

        $response = $this->jsonAs($this->user, 'GET', route('activity.index'))
            ->assertStatus(200);

        $response->assertJsonFragment([
            'type' => 'created_team',
            'uuid' => $this->user->uuid,
        ]);
    }

If I need to share any more info please let me know. Thanks!

L. Fox
  • 328
  • 1
  • 6
  • 20
  • so the post request at the start of your function should be saving an associated activity of the using having created that page? If you perform this step manually, is an activity being logged? – Alec Joy Oct 30 '19 at 14:54
  • Yes. Also if I disable `RefreshDatabase` and run it, it passes and I can see the activity record in the DB. – L. Fox Oct 30 '19 at 14:58
  • Does the error still occur you use DatabaseTransactions instead of RefreshDatabase? Import `use Illuminate\Foundation\Testing\DatabaseTransactions;` at the top and change `use RefreshDatabase` to `use DatabaseTransactions` Also check your log files for errors when running these tests, because the activity is created with a lifecycle hook it's possible for that hook to be failing in the background – Alec Joy Oct 30 '19 at 15:01
  • That did the trick! Thanks! If you want to add that as the answer I'll mark it for you – L. Fox Oct 30 '19 at 15:07
  • In this case I don't think that suggestion would make a very good answer without figuring out why it fails when using RefreshDatabase. You've got other things to work on so I won't ask you to do a bunch of debugging but can you check the logs for any errors pertaining to creating the activity? – Alec Joy Oct 30 '19 at 15:10
  • The logs have this `failed to open stream: No such file or directory {"userId":6,"exception":"[object] (ErrorException(code: 0): include(/Users/loganfox/code/personality.test/vendor/composer/../../app/Activity.php): failed to open stream: No such file or directory at /Users/loganfox/code/personality.test/vendor/composer/ClassLoader.php:444)` – L. Fox Oct 30 '19 at 15:15
  • Well that is...not what I expected haha. I'd love to get to the bottom of this but you've got other stuff to do, and it's working for you now so no reason to dive deeper. If you run in to a similar issue again come back to this thread and we'll try to figure out the root cause. – Alec Joy Oct 30 '19 at 15:24
  • I have a little time if you want to move this over to a chat? – L. Fox Oct 30 '19 at 15:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201615/discussion-between-l-fox-and-alec-joy). – L. Fox Oct 30 '19 at 15:45
  • @AlecJoy have you figured anything out? – L. Fox Oct 30 '19 at 18:10
  • I haven't been able to replicate it on a fresh install of 5.8 or 6.2 so I'm not sure. Can you post just the top portion of your Activity class? from the opening – Alec Joy Oct 30 '19 at 18:13
  • Hmm, it's definitely the namespace but I'm not sure why it would work in DatabaseTranscations and not work in RefreshDatabase. It seems to be just flat out ignoring your namespace when run with RefreshDatabase. I still think this has something to do with the way the app is bootsrapped when using RefreshDatabase but I'm not sure what the difference would be. – Alec Joy Oct 30 '19 at 19:15

0 Answers0