cjstehno / ersatz

🤖 A simulated HTTP server for testing client code with configurable responses.
https://cjstehno.github.io/ersatz
Apache License 2.0
47 stars 5 forks source link

Why the expectation logs in falure situation has unexpected output? #108

Closed havenqi closed 5 years ago

havenqi commented 5 years ago

It's good to see the details of the expectation logs. But I'm confused with expectation 0, and 1, because the test only has expecation 2 configured. Could you explain more? Thanks.

image

def "Posting 1"() {
        setup:
        server.expectations {
            post('/posting') {
                body BODY_CONTENT, TEXT_PLAIN               // if BODY_CONTENT is used here, got '404: Not Found' due to failure of matching. Instead 'abc' works
                decoder TEXT_PLAIN, getUtf8String()

                responder {
                    body BODY_CONTENT                          // if BODY_CONTENT is used her, got bad response: 'Expectations (ErsatzRequest): <GET>, "BODY_CONTENT"'. Instead 'abc' works
                    encoder(TEXT_PLAIN, String) {
                        obj -> obj
                    }
                }
            }
        }.start()

        when:
        String value = exec(clientPost('/posting', 'text/plain; charset=utf-8', BODY_CONTENT).build()).body().string()

        then:
        value == BODY_CONTENT

    }
cjstehno commented 5 years ago

How are you configuring the server itself? Are you sure it is being cleaned up between tests? Something like:

@AutoCleanup 
private final ErsatzServer ersatzServer = new ErsatzServer()

If not, this could cause the extra expectations, since they are not being cleaned out in a reused server.

cjstehno commented 5 years ago

Also, just FYI, calling expectations calls start() unless autoStart false is specified, so your explicit start() call is not needed.

cjstehno commented 5 years ago

Well, seeing the code in your other issue, it appears you are cleaning things up as you should, perhaps I broke something in the recent update. I will take a look.

As a temporary fix, try adding the following to your test class:

def cleanup(){
    server.clearExpectations()
}

This will clear out the expectations between tests, as it should.

Thanks for reporting.

cjstehno commented 5 years ago

It appears that this is directly related to your other issue #107 - when the Groovy closure variable resolution gets confused it does some odd things. The response body DSL method calls the request body object in this case which creates another request expectation. I am going to close this issue and address both in the other ticket.