derrickrozay's avatar

Laravel Unit Test - how to get meaningful output?

I am using Laravel 5.8 and I am trying to run tests using phpunit in my console but I keep getting Expected status code 200 but received 409. Failed asserting that false is true.. I have validation errors set to return 409, however this error tells me nothing. How can I output or view more meaningful output like the full exception instead of this error message which doesn't help me at all. I have tried to disable the default error reporting but its not working $this->withoutExceptionHandling();

SaveResults Controller

public function save(Request $request) {

    $model = new User();

    $data = $request->all(); 
    $now = Carbon::now();
    $validator = Validator::make($data, $model->getValidationRules());
    // Here is where the error is thrown
    if($validator->fails()) {
        return response()->json(['error' => $validator->errors()->first()], 409);
}

Test.php

public function testCreateCachedData() {

    $this->withoutExceptionHandling();

    $this->withoutExceptionHandling();
    $user = factory(User::class)->create();
    $userRepo = factory(UserRepo::class)->create(['c_id' => $user->id]);


    $response = $this->post('/api/users/save', $user->toArray());
    $response->assertStatus(200);
}
0 likes
3 replies
ftiersch's avatar

You are making a call to your application and receive a response that doesn't have the status code you expect.

  1. The error messages are in your $response object ($response->getContent() I believe)
  2. Your error probably comes from the second $ in $$user->toArray() which is weird that it's not recognized straight away as an error
derrickrozay's avatar

Thanks but the second $ wasn;t the problem I must have accidentally pasted that.

How would I use $response->getContent()? Just dd($response->getContent())?

ftiersch's avatar

You could do that but that would stop all tests :) So it's okay for "debugging" the tests. But you can usually pass another parameter to the assert methods to put out your own message.

$response->assertStatus(200, "Response is: " . $response->getContent());

Something like that (depending on the situation). Then you your tests will keep running but you get more output.

Please or to participate in this conversation.