Autofixture.Automoq - generics make bool always true

public interface IResult
{
    bool Success { get; } 
}

public interface IResult<T> : IResult
{

}

Using AutoFixure and AutoMoq, I'm trying to find a way to do it Successalways true, regardless of type T. Registering a fake is fairly simple with IResult, but it doesn’t work forIResult<T>

+4
source share
1 answer

Using fake implementation

public class FakeResult<T> : IResult<T> {
    public bool Success {
        get { return true; }
    }
}

along with adding settings TypeRelay

 fixture.Customizations.Add(new TypeRelay(typeof(IResult<>), typeof(FakeResult<>)));

All calls IResult<>will use FakeResult<>one that has Successto return trueregardless of type T.

A complete example to verify that the layout works as intended.

[TestClass]
public class AutoFixtureDefaultGeneric {
    [TestMethod]
    public void AutoFixture_Should_Create_Generic_With_Default() {
        // Arrange
        Fixture fixture = new Fixture();
        fixture.Customizations.Add(new TypeRelay(typeof(IResult<>), typeof(FakeResult<>)));

        //Act
        var result = fixture.Create<IResult<string>>();

        //Assert
        result.Success.Should().BeTrue();
    }
}
+5
source

Source: https://habr.com/ru/post/1696249/


All Articles