Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for verifying a request was made #35

Closed
dpix opened this issue Jun 26, 2017 · 5 comments
Closed

Support for verifying a request was made #35

dpix opened this issue Jun 26, 2017 · 5 comments

Comments

@dpix
Copy link

dpix commented Jun 26, 2017

Firstly, this is a great library 👍

It would be great to have a verify method somewhat like that in Moq. So that I can verify that my HttpClient made was called at all with the correct URL, regardless of response. Could also include functionality for expecting a particular response.

e.g.

new MockHttpMessageHandler().VerifyUrlFetched("some url", Times.Once);

Is this something you would consider adding to the API?

@richardszalay
Copy link
Owner

This functionality is somewhat supported using Expect instead of When, but that's more akin to an ordered mock than a post-verify.

Could you talk through the details of your scenario so I can understand it better?

@dpix
Copy link
Author

dpix commented Jun 30, 2017

I want to be able to test that the url requested was expected for the scenario, regardless of the response. Expect still requires me to check the response.

e.g. (pseudo code)

void MethodToTest(){
    var url = someMethodThatGeneratesAUrl();
    if(someCondition){
        httpClient.GetAsync(url);
    }

   ......
}

Then in my test, all I want to do is check that GetAsync was called exactly one time with the correct url given the condition.

void TestMethod(){
    MethodToTest();
    mockedClient.VerifyUrl(theUrlExpected, Times.Once); // or Times.Never if I expect it not to get called
}

Does that explain it a little better? Happy to help out with a PR, am curious if this is functionality you would consider adding.

@richardszalay
Copy link
Owner

This was just released in 3.2.0

@richardszalay
Copy link
Owner

richardszalay commented Aug 31, 2017

Hi @dpix,

Just to follow up, the implementation I ended up going for was to track the number of times a specific IMockedRequest (returned by When / Expect) was matched.

The reason I went in this direction is that using a URL would have raised questions around matching with/without querystring (or the order of keys), which would lead back to IMockedRequest anyway.

The reason it matches against an existing instance of IMockedRequest, rather than simply evaluating the matched HttpRequestMessages against the supplied mocked request, is that a number of the matchers require access to the Content and that may no longer be available.

Hope that make sense. I've also added an example to the README:

var mockHttp = new MockHttpMessageHandler();

var request = mockHttp.When("http://localhost/api/user/*")
        .Respond("application/json", "{'name' : 'Test McGee'}");

var client = mockHttp.ToHttpClient();

await client.GetAsync("http://localhost/api/user/1234");
await client.GetAsync("http://localhost/api/user/2345");
await client.GetAsync("http://localhost/api/user/3456");

Console.Write(mockHttp.GetMatchCount(request)); // 3

PS. I do generally promote PRs and discussion around features, but I haven't had a lot of time to work on the library so when I had the chance I ran through all of the feature I thought I could do at one time.

@dpix
Copy link
Author

dpix commented Sep 7, 2017

Looks great @richardszalay! I'll make sure to update and check it out next I'm updating the tests I was working on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants