0

I am new to Laravel and also unit testing in general.

I have written a simple test using the guide from Laravel,

public function testGet(){
    $this->get('/api/accounts/1')->assertStatus(200)->assertJsonStructure([
        'status','data'
    ]);
}

When the test is run, it generates the following failure (which is intended):

Time: 127 ms, Memory: 12.00MB
There was 1 failure:

1) Tests\Unit\AccountApiTest::testGet
Invalid JSON was returned from the route.

My question is, is there any way we can customize the message generated when a test fails for all the available assertion in Laravel? For this case, the message

Invalid JSON was returned from the route.

1 Answer 1

1

No, you cannot modify the message easily as it is hardcoded in the Illuminate/Foundation/Testing/TestResponse.php:

/**
 * Validate and return the decoded response JSON.
 *
 * @param  string|null  $key
 * @return mixed
 */
public function decodeResponseJson($key = null)
{
    $decodedResponse = json_decode($this->getContent(), true);

    if (is_null($decodedResponse) || $decodedResponse === false) {
        if ($this->exception) {
            throw $this->exception;
        } else {
            PHPUnit::fail('Invalid JSON was returned from the route.');
        }
    }

    return data_get($decodedResponse, $key);
}

You may be able to get custom messages by overwriting the TestResponse.

Sign up to request clarification or add additional context in comments.

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.