lipanski / mockito

HTTP mocking for Rust!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

expect 0 fails on assert

vasilakisfil opened this issue · comments

If one sets expect(0) and then asserts the mock, an error will appear: could not retrieve enough information about the remote mock. The same goes for matched.

Apparently, the code inside Mock always checks for get_mock_hits (which is always None in the aforementioned case) and assumes that it will/should be Some.

Sounds like a bug to me, or at least, a limitation. One should be able to assert that no requests were made to a 3rd part service.

Expecting 0 definitely works:

#[test]
fn test_expect_zero() {
    let mut s = Server::new();
    let mock = s.mock("GET", "/hello").expect(0).create();

    mock.assert();
}

#[test]
#[should_panic(expected = "\n> Expected 0 request(s) to:\n\r\nGET /hello\r\n\n...but received 1\n")]
fn test_expect_zero_fail() {
    let mut s = Server::new();
    let host = s.host_with_port();
    let mock = s.mock("GET", "/hello").expect(0).create();

    request(&host, "GET /hello", "");

    mock.assert();
}

...so there's something else happening there. Are you sure you've called .create() or .create_async() correctly? Or are you mixing sync/async code by any chance? Could you share some code to reproduce this?

Indeed you are right, I wasn't calling the create method. Potentially a #[must_use] could be helpful here.

There used to be a #[must_use] but now that we have a create_async() method on top it became obsolete 😿

I'll see if I can tune the error messages.