Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created April 15, 2026 12:51
Show Gist options
  • Select an option

  • Save dcomartin/eee179995cc855ed2f421e7218644bb0 to your computer and use it in GitHub Desktop.

Select an option

Save dcomartin/eee179995cc855ed2f421e7218644bb0 to your computer and use it in GitHub Desktop.
public static class VinDecoderFixture
{
public static VinDecoder CreateSuccessful(
int year = 2026,
string make = "Ford",
string model = "F-150")
{
var json = $$"""
{
"year": {{year}},
"make": "{{make}}",
"model": "{{model}}",
}
""";
var handler = new StubHttpMessageHandler(_ =>
new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
});
var httpClient = new HttpClient(handler);
return new VinDecoder(httpClient);
}
public static VinDecoder CreateFailure(
HttpStatusCode statusCode = HttpStatusCode.BadRequest,
string json = """{ "error": "invalid request" }""")
{
var handler = new StubHttpMessageHandler(_ =>
new HttpResponseMessage(statusCode)
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
});
var httpClient = new HttpClient(handler);
return new VinDecoder(httpClient);
}
}
public class Tests
{
[Fact]
public async Task Handle_returns_vehicle_name()
{
var vinDecoder = VinDecoderFixture.CreateSuccessful(
year: 2026,
make: "Ford",
model: "F-150");
var sut = new RegisterVehicleHandler(vinDecoder);
var result = await sut.Handle(new RegisterVehicleCommand("123456789"), CancellationToken.None);
Assert.Equal("2026 Ford F-150", result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment