Comments (6)
OK, further investigation shows that this appears to be because I haven't actually called the Expect() yet:
[Fact]
public async void This_Test_Will_Pass() {
const int ID = 123;
var mockHttp = new MockHttpMessageHandler();
mockHttp.Expect(HttpMethod.Post, $"https://api.test/{ID}/foo").Respond(HttpStatusCode.Created);
mockHttp.When(HttpMethod.Post, $"https://api.test/{ID}/bar").Respond(HttpStatusCode.Created);
var httpClient = mockHttp.ToHttpClient();
await httpClient.PostAsync("https://api.test/123/foo", new StringContent(""));
var response = await httpClient.PostAsync("https://api.test/123/bar", new StringContent(""));
response.EnsureSuccessStatusCode();
}
I'd assumed the fallback behaviour from Expect
to When
was an a per-URL-per-method basis, but it looks like what's actually happening is that as long as there are unsatisifed Expect()
expectations, any call that would be handled by a When
is going to return a 404 NotFound
- is that correct?
from mockhttp.
...and there's the bit of the docs that didn't make any sense until I'd found this out the hard way:
but they won't match if there are any outstanding Request Expectations present (unless BackendDefinitionBehavior.Always is specified). If no Request Expectations match, Fallback will be used.
from mockhttp.
Would you be interested in a PR that would make the error message more explicit in this particular scenario? I realised it's covered in the documentation but I'd be happy to take a look at making the error message more descriptive so that other folks don't end up in the same situation I found myself in.
from mockhttp.
I just bumped into the exact same problem @dylanbeattie . Thanks for the info, I can fix my issue now. Clearer docs would help here.
from mockhttp.
Happy to make things clearer, but could you elaborate on why the When vs Expect and Match Behavior parts of the README aren't clear enough?
from mockhttp.
I'm going to close this ticket, but if you have time to elaborate on how the linked parts of the readme could be made clearer I'm happy to imprive the docs.
from mockhttp.
Related Issues (20)
- Documentation Request HOT 3
- Adding Response Headers
- Question: Matching Query with XML HOT 4
- Feature Request: Support BaseAddress and DefaultRequestHeaders HOT 3
- Not Compatible with Sync HttpClient Send HOT 4
- Matcher async support HOT 4
- How to add additional conditions to .When()? HOT 1
- HttpClientJsonExtensions.PostAsJsonAsync Throws NullReferenceException When Serializing the Property/Value Message = "Message:" HOT 2
- Package does not support URI containing fragments HOT 3
- Not seeing anyway to follow redirects HOT 2
- Does not support URL-encoded content in URIs HOT 2
- Lib cannot be used in .NET7 projects HOT 3
- IMockedRequestMatcher should have MatchesAsync HOT 2
- Helper to read JSON file as response HOT 2
- Multiple requests fail on version 7 when using Expect HOT 5
- Overwrite same endpoint with different result HOT 3
- Feature request: clear match counts HOT 4
- Can't Match Using WithFormData, WithHeaders HOT 5
- Library status HOT 1
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 mockhttp.