Question

Please note, I'm somewhat new to TDD, so I will take general advice as well as specific answer.

Neither abstract classes nor interfaces can be instantiated. Clearly Moq can give me a mocked up instance of the ADataFeed in the second test. Why does AutoMoqCustomization work for interfaces IDataFeed but not for abstract classes ADataFeed, instead throwing an InvalidOperationException?

Secondarily, what would be the AutoFixture approach (or TDD generally) be to drive a design that might call for an abstract class with a constructor to require and guarantee certain values, such as a connection string in this case?

[Theory, AutoMoqData]
public void AllDataFeedsHaveAConectionString(
    IDataFeed sut)
{
    var result = sut.GetConnectionString();
    Assert.Null(result);
}

[Fact]
public void AllDataFeedsRequireAConnectionString()
{
    var expected = Guid.NewGuid().ToString();
    var sut = new Mock<ADataFeed>(expected);
    var result = sut.Object.GetConnectionString();
    Assert.Equal(expected, result);
}

[Theory, AutoMoqData]
public void AllDataFeedsRequireAConnectionString2(
    [Frozen] string expected, 
    ADataFeed sut)
{
    var result = sut.GetConnectionString();
    Assert.Equal(expected, result);
}
Was it helpful?

Solution

Abstract classes with constructors must be marked protected. AutoFixture will not program against abstract classes when the constructor is marked public, as this is a design error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top