简体   繁体   中英

Using MockHttpClient nuget package in VS 2017

I am trying to test an adapter service in VS 2017 in C#. My test is failing because it is wanting a 400 through 499 response from the HTTPClient . When my test runs the service returns a 500.

So searching I found MockHttpClient nuget package but the examples given are not working when I try them in my test.

example: https://github.com/codecutout/MockHttpClient/blob/master/README.md

I get an error saying

'MockHttpClient' is a namespace but is used like a type

I also added in a using MockHTTPClient at the top of my test.

What am I doing wrong?

getting error with the below

var mockHttpClient = new MockHttpClient();
mockHttpClient.When("the url I am using").Returns(HttpStatusCode.Forbidden)

It's a name clash with the namespace. The class and namespace share the same name.

Remove the using statement and use this instead:

var mockHttpClient = new MockHttpClient.MockHttpClient();

Poor choice of names for this library and a horrific amount of dependencies. I would stay away if I were you.

UPDATE:

You asked for an alternative so here is what I recently did for a project:

The HttpClient class has a constructor that takes an HttpMessageHandler object, so you can pass your own handler and simulate the behavior.

Create a class that derives from DelegatingHandler and overrides the send behavior:

public class TestHandler : DelegatingHandler
{
    private Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _handler;

    public TestHandler(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handler)
    {
        _handler = handler;
    }

    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        return _handler(request, cancellationToken);
    }

    public static Task<HttpResponseMessage> OK()
    {
        return Task.Factory.StartNew(() => new HttpResponseMessage(HttpStatusCode.OK));
    }

    public static Task<HttpResponseMessage> BadRequest()
    {
        return Task.Factory.StartNew(() => new HttpResponseMessage(HttpStatusCode.BadRequest));
    }
}

Then on your test, you use your handler in the constructor:

//Create an instance of the test handler that returns a bad request response
var testHandler = new TestHandler((r, c) =>
{                
    return TestHandler.BadRequest();
});

//Create the HTTP client
var client = new HttpClient(testHandler);

//Fake call, will never reach out to foo.com
var request = new HttpRequestMessage(HttpMethod.Get, "http://www.foo.com");
request.Content = new StringContent("test");

//This will call the test handler and return a bad request response
var response = client.SendAsync(request).Result;

Notice I have a couple of convenience static methods in there to create the handling functions for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM