3

i have a problem to this test:

$this->json('POST', 'api/login')
        ->assertStatus(422)
        ->assertJson([
            'email' => ['The email field is required.'],
            'password' => ['The password field is required.'],
        ]);

And I don't understand what the error:

Unable to find JSON: 

[{
    "email": [
        "The email field is required."
    ],
    "password": [
        "The password field is required."
    ]
}]

within response JSON:

[{
    "message": "The given data was invalid.",
    "errors": {
        "email": [
            "The email field is required."
        ],
        "password": [
            "The password field is required."
        ]
    }
}].


Failed asserting that an array has the subset Array &0 (
    'email' => Array &1 (
        0 => 'The email field is required.'
    )
    'password' => Array &2 (
        0 => 'The password field is required.'
    )
).
--- Expected
+++ Actual
@@ @@
    0 => 'The password field is required.',
    ),
),
-  'email' => 
-  array (
-    0 => 'The email field is required.',
-  ),
-  'password' => 
-  array (
-    0 => 'The password field is required.',
-  ),
)

It seems that the JSON assert is within the answer.

2
  • What version of Laravel are you using? Commented May 13, 2019 at 11:16
  • I'm using Laravel 5.8.15. I solve using assertJsonFragment Commented May 13, 2019 at 11:32

2 Answers 2

6

assertJson won't work in the case as the data you're looking for is under errors.

You can either wrap your array and key it with "errors":

->assertJson([
    'errors' => [
        'email'    => ['The email field is required.'],
        'password' => ['The password field is required.'],
    ],
])

or you could instead use assertJsonFragment which will try and match any part of the json to what you've provided:

->assertJsonFragment([
    'email' => ['The email field is required.'],
    'password' => ['The password field is required.'],
])
Sign up to request clarification or add additional context in comments.

Comments

0

Confirm that the string you're checking against matches the one in the assert method.

I had the same problem as illustrated below

The string I was checking against: Reimbursement updated successfully! The string in my assert statement: Reimbursement updated successfully

Note the missing !, the two strings/values have to be an exact match!

~Regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.