hotmeteor / spectator

OpenAPI testing for PHP
MIT License
285 stars 53 forks source link

No response object matching returned status code [200] when 201 is expected #121

Closed philsturgeon closed 9 months ago

philsturgeon commented 2 years ago

I'm being told that there's no 200 status code so a test is failing, but it actually is responding with a 200. It's meant to be 201, which is what my test is checking for (my controller was wrong).

  • Tests\Feature\OrdersCreateTest > it creates an order
   ErrorException

  No response object matching returned status code [200].
Failed asserting that true is false.

  at tests/Feature/OrdersCreateTest.php:39
     35▕         ->postJson('/orders/', [
     36▕             'orderedTrees' => 1,
     37▕         ])
     38▕         ->assertValidRequest()
  ➜  39▕         ->assertValidResponse(201);
     40▕ });
     41▕

  1   tests/Feature/OrdersCreateTest.php:39
      Illuminate\Testing\TestResponse::__call()

So Spectator is detecting the response doesn't match the expectated status code, its just reporting it wrong in the error message.


it('creates an order', function () {
    $this
        ->withToken($this->api_key)
        ->postJson('/orders/', [
            'orderedTrees' => 1,
        ])
        ->assertValidRequest()
        ->assertValidResponse(201);
});
hotmeteor commented 2 years ago

What the message is saying is "The spec doesn't have a 200 response defined"... unless you're saying it does?

Either way, I think the message could be improved to say:

The status code provided (200) does not match any defined response objects.

Or something to that effect.

philsturgeon commented 2 years ago

Reading it now it makes sense, but was super confused at the time.

Can we do something like expected vs received?

Expected status code 201, received status code 200.

Or something?

There's a fair few assertions that are Failed to assert that true equals false and I think language like this is usually more clear.