linkrope / dunit

xUnit Testing Framework for D
Boost Software License 1.0
37 stars 9 forks source link

Assertions assertInitial, assertNotInitial #26

Open andre2007 opened 4 years ago

andre2007 commented 4 years ago

I would like to test whether a struct is initial or not.

As workaround assertTrue, assertSame could be used but two new assertions assertInitial and assertNotInitial would be more expressive.

linkrope commented 4 years ago

Just out of curiosity: what's the use case for such assertions?

To express the intent, an alias could help. Also, the error message could be improved that way:

alias assertInitial = actual => assertEqual(actual, typeof(actual).init, "not inital");
andre2007 commented 4 years ago

In my test I want to check whether the http service call was triggered. The http client is mocked for the test and while it is triggered a delegate is called. In the delegate I stored the Request structure and with assertInitial I want to check whether it was filled:

@Test void createBinding()
{
    Request request;

    requests.setNoContentResponse(HttpMethod.post, "/apps/myapp/bindings", (r) {
        request = r;
    });

    cut.execute(["myapp", "--binding", "b1"]);
    assertFalse(request == Request.init);
}
linkrope commented 4 years ago

OK: this is a problem related to mocking. With hand-made mocks, there are several approaches:

I don't like the idea of supporting "approach 3" with a rather special assertion. So, I would suggest that you try the alias proposal.

BTW: we abandoned hand-made mocks in favour of a mocking framework: https://github.com/funkwerk/dmocks.