Comments (3)
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.
🤔 hmm.. i tried to do that but kept getting something like "no response was returned" or something .. hmm ...
from moq.contrib.httpclient.
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)
- Question: In the handler.VerifyRequest ... HOT 1
- Method for validating request Content HOT 2
- Issues with 'ReturnsJsonResponse' and Deserializing the Response-Content HOT 2
- Compiler warning CS8002: Referenced assembly does not have a strong name HOT 2
- Unsupported expression: x => x.CreateClient() Extension methods (here: HttpClientFactoryExtensions.CreateClient) may not be used in setup / verification expressions. HOT 12
- Repeated calls to SetupRequest only returns reponse once, then fails HOT 3
- Use in TestServer HOT 13
- Dotnet 7: The value cannot be null or empty. (Parameter 'mediaType') HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moq.contrib.httpclient.