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);
}
AutoMoqCustomization
can provide abstract base classes as well as interfaces. Is the constructor ofADataFeed
public? If so, make it protected. It's a design error with a public constructor on an abstract class – Jez