Skip to content
Pure Krome edited this page Apr 26, 2017 · 5 revisions

Wildcard endpoints

What: faking either a single or multiple requests but it is for any request endpoint.

Why: you're don't care about the endpoint OR it might be different each time. Or you're being slack :)

How: Create a fake response but don't define the request endpoint.

Ok - so here's a simple example of saying "I don't care what the endpoint is - at least we've hit something.".

[Fact]
public async Task GivenSomeValidHttpRequest_GetSomeFooDataAsync_ReturnsAFoo()
{
    // Arrange.

    // Fake response.
    const string responseData = "{ \"Id\":69, \"Name\":\"Jane\" }";
    var messageResponse = FakeHttpMessageHandler.GetStringHttpResponseMessage(responseData);

    // Prepare our 'options' with all of the above fake stuff.
    var options = new HttpMessageOptions
    {
        RequestUri = null, // Of course, you can just ignore this property because it
                           // defaults to null anyways.
        HttpResponseMessage = messageResponse
    };

    // 3. Use the fake response if that url is attempted.
    var messageHandler = new FakeHttpMessageHandler(options);

    var myService = new MyService(messageHandler);

    // Act.
    // NOTE: network traffic will not leave your computer because you've faked the response, above.
    var result = await myService.GetSomeFooDataAsync();

    // Assert.
    result.Id.ShouldBe(69); // Returned from GetSomeFooDataAsync.
    result.Baa.ShouldBeNull();
    options.NumberOfTimesCalled.ShouldBe(1);
}