Skip to content

Latest commit

 

History

History

MockHttp

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Kralizek.AutoFixture.Extensions.MockHttp

This package is meant to provide an easy integration between AutoFixture and MockHttp.

Usage

You can use this extension by simply invoking the AddMockHttp method on the IFixture instance at hand.

var fixture = new Fixture().AddMockHttp();

var testUri = fixture.Create<Uri>();
var expectedResult = fixture.Create<string>();

var handler = fixture.Freeze<MockHttpMessageHandler>();
handler.When(HttpMethod.Get, testUri.ToString()).Respond(HttpStatusCode.OK, new StringContent(expectedResult));

var sut = fixture.Create<TestService>();

var result = await sut.GetString(testUri);

Assert.That(result, Is.EqualTo(expectedResult));            

Alternatively, you can use a customized version of the AutoData attribute to improve the readibility of your unit test.

[AttributeUsage(AttributeTargets.Method)]
public class TestAutoDataAttribute : AutoDataAttribute
{
    public TestAutoDataAttribute() : base (() => new Fixture().AddMockHttp()) { }
}

[Test, TestAutoData]
public async Task Custom_AutoData_should_work([Frozen] MockHttpMessageHandler handler, TestService sut, Uri testUri, string expectedResult)
{
    handler.When(HttpMethod.Get, testUri.ToString()).Respond(HttpStatusCode.OK, new StringContent(expectedResult));

    var result = await sut.GetString(testUri);

    Assert.That(result, Is.EqualTo(expectedResult));
}

Credits

The code in this package was provided by Andrei Ivascu.