Justus250422's avatar

assertJson() doesn't work as expected

Hi. I am using Laravel 8. I'm writing tests for a project, I'm new to this. I am using assertJson to check if sending a request returns a valid result. The snag is that this test returns once that everything is ok and once that it is wrong, randomly. Sometimes it finds what it needs to and sometimes it doesn't. Below is the information needed:

Test:

public function test_user_with_read_workregisters_permission_should_not_see_entries_of_users_from_other_party()
    {
      // GIVEN
      setPermissionsTeamId($this->party->id);

      $adminUser = $this->adminUser;
      $normalUser = $this->normalUser;
      $adminUserFromAnotherParty = $this->adminUserFromAnotherParty;

      $this->actingAs($adminUser);

      $adminUserEntry = WorkRegister::factory()->create([
        'user_id' => $adminUser->id,
        'title' => 'adminUserEntry',
        'party_id' => $this->party->id,
        'creator_id' => $adminUser->id,
      ]);

      $normalUserEntry = WorkRegister::factory()->create([
        'title' => 'normalUserEntry',
        'user_id' => $normalUser->id,
        'party_id' => $this->party->id,
        'creator_id' => $normalUser->id,
      ]);

      $entryFromAnotherParty = WorkRegister::factory()->create([
        'user_id' => $adminUserFromAnotherParty->id,
        'title' => 'adminUserEntryFromOtherParty',
        'party_id' => $this->anotherParty->id,
        'creator_id' => $adminUserFromAnotherParty->id,
      ]);

      $adminUser->assignRole(PermissionDictionary::ADMIN_ROLE['name']);

      //WHEN
      $response = $this->actingAs($adminUser)->getJson("api/workregisters");

      $response->assertStatus(200);
      $response->assertJson([
        'data' => [
          [
            'user_id' => $adminUser->id,
            'title' => $adminUserEntry->title,
            'project_id' => $adminUserEntry->project_id,
            'duration' => $adminUserEntry->duration,
            'party_id' => $this->party->id,
          ],
          [
            'user_id' => $normalUser->id,
            'title' => $normalUserEntry->title,
            'project_id' => $normalUserEntry->project_id,
            'duration' => $normalUserEntry->duration,
            'party_id' => $this->party->id,
            ]
        ]
      ]);
      $response->assertJsonMissing([
        'user_id' => $adminUserFromAnotherParty->id,
        'title' => $entryFromAnotherParty->title,
        'project_id' => $entryFromAnotherParty->project_id,
        'duration' => $entryFromAnotherParty->duration,
        'party_id' => $this->anotherParty->id,
      ]);
    }

Error:

  Unable to find JSON: 
  
  [{
      "data": [
          {
              "user_id": 5,
              "title": "adminUserEntry",
              "project_id": 3,
              "duration": 3600,
              "party_id": 11
          },
          {
              "user_id": 6,
              "title": "normalUserEntry",
              "project_id": 4,
              "duration": 1800,
              "party_id": 11
          }
      ]
  }]

  within response JSON:
  
  [{
      "current_page": 1,
      "data": [
          {
              "id": 4,
              "user_id": 6,
              "party_id": 11,
              "project_id": 4,
              "title": "normalUserEntry",
              "duration": 1800,
             // Other values
          },
          {
              "id": 3,
              "user_id": 5,
              "party_id": 11,
              "project_id": 3,
              "title": "adminUserEntry",
              "duration": 3600,
             // Other values
          }
             // Other values
  }].

  at tests/Feature/WorkRegisterControllerTest.php:156
    152▕             'user_id' => $normalUser->id,
    153▕             'title' => $normalUserEntry->title,
    154▕             'project_id' => $normalUserEntry->project_id,
    155▕             'duration' => $normalUserEntry->duration,
  ➜ 156▕             'party_id' => $this->party->id,
    157▕             ]
    158▕         ]
    159▕       ]);
    160▕       $response->assertJsonMissing([
  --- Expected
  +++ Actual
  @@ @@
       0 => 
       array (
         'id' => 4,
  -      'user_id' => 5,
  +      'user_id' => 6,
         'party_id' => 11,
  -      'project_id' => 3,
  -      'title' => 'adminUserEntry',
  -      'duration' => 3600,
  +      'project_id' => 4,
  +      'title' => 'normalUserEntry',
  +      'duration' => 1800,
         'duration_excluded' => 1800,
         'duration_exclude_all' => 0,
         'workdate' => '2023-08-06 14:46:21',
  @@ @@
       1 => 
       array (
         'id' => 3,
  -      'user_id' => 6,
  +      'user_id' => 5,
         'party_id' => 11,
  -      'project_id' => 4,
  -      'title' => 'normalUserEntry',
  -      'duration' => 1800,
  +      'project_id' => 3,
  +      'title' => 'adminUserEntry',
  +      'duration' => 3600,
         'duration_excluded' => 3600,
         'duration_exclude_all' => 0,
         'workdate' => '2023-08-29 09:09:57',

assertJson confuses the order and checks incorrect results with each other. Why? Thank you.

0 likes
1 reply

Please or to participate in this conversation.