Git Product home page Git Product logo

Comments (6)

dylanbeattie avatar dylanbeattie commented on June 12, 2024

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.

dylanbeattie avatar dylanbeattie commented on June 12, 2024

...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.

dylanbeattie avatar dylanbeattie commented on June 12, 2024

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.

mosoftwareenterprises avatar mosoftwareenterprises commented on June 12, 2024

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.

richardszalay avatar richardszalay commented on June 12, 2024

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.

richardszalay avatar richardszalay commented on June 12, 2024

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)

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.