Git Product home page Git Product logo

Comments (3)

maxkagamine avatar maxkagamine commented on June 1, 2024 1

Hi! You can use the standard Moq Throws() to cause the handler to throw an exception:

var handler = new Mock<HttpMessageHandler>();
var client = handler.CreateClient();

handler.SetupAnyRequest()
    .Throws<HttpRequestException>();

Func<Task> attempt = () => client.GetAsync("http://example.com");
await attempt.Should().ThrowAsync<HttpRequestException>();

Or if you want to get fancy:

var inner = new System.Net.Sockets.SocketException((int)System.Net.Sockets.SocketError.ConnectionRefused);
handler.SetupAnyRequest()
    .Throws(new HttpRequestException(inner.Message, inner));

from moq.contrib.httpclient.

PureKrome avatar PureKrome commented on June 1, 2024

🤔 hmm.. i tried to do that but kept getting something like "no response was returned" or something .. hmm ...

from moq.contrib.httpclient.

maxkagamine avatar maxkagamine commented on June 1, 2024

Expected System.Net.Http.HttpRequestException, but found System.InvalidOperationException with message "Handler did not return a response message." ?

Try new Mock<HttpMessageHandler>(MockBehavior.Strict); and see if you get a MockException "All invocations on the mock must have a corresponding setup". Might just not be matching the setup.

from moq.contrib.httpclient.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.