Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does AutoMoqCustomization work for abstract classes?

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);
}
like image 709
cocogorilla Avatar asked Dec 14 '12 20:12

cocogorilla


People also ask

Do abstract classes allow inheritance?

It cannot be instantiated, or its objects can't be created. A class inheriting the abstract class has to provide the implementation for the abstract methods declared in the abstract class. An abstract class can contain constructors, static methods, and final methods as well.

Are abstract classes useless?

No, They are not obsolete. In fact, there is an obscure but fundamental difference between Abstract Classes/Methods and Interfaces. if the set of classes in which one of these has to be used have a common behaviour that they share (related classes, i mean), then go for Abstract classes/methods.

Are abstract classes used as building blocks?

In Java, the Abstract classes and interfaces are fundamental building blocks as they both are used to implement one of the essential concepts of Object-Oriented Programming that is, Abstraction.

How is abstract class different from Aninterface?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.


1 Answers

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.

like image 129
cocogorilla Avatar answered Oct 10 '22 07:10

cocogorilla