Context
I've just came across a scenario where I have multiple UnitTests
that are similiar enough to be almost handled in a ctrl-c ctrl-v
way. For example, when asserting that AutoMapper
and its Profiles
are valid.
Question
So I've been thinking if it would be considered a good practice to extract an Interface from those tests and implement it whenever I need to test a new Class that falls within the same purpose.
Code Snippet
For example, for the following Tests:
[Fact]
public void ConfigurationValid()
{
throw new System.NotImplementedException();
}
[Fact]
public void FromCreate()
{
throw new System.NotImplementedException();
}
[Fact]
public void FromEdit()
{
throw new System.NotImplementedException();
}
[Fact]
public void ToDetail()
{
throw new System.NotImplementedException();
}
[Fact]
public void ToList()
{
throw new System.NotImplementedException();
}
[Fact]
public void ToList_Many()
{
throw new System.NotImplementedException();
}
Could be extracted to the following interface:
public interface IViewModelsTests
{
void ConfigurationValid();
void FromCreate();
void FromEdit();
void ToDetail();
void ToList();
void ToList_Many();
}
And whenever I create a new ViewModel and wish to unit test it I can just implement the interface on my new TestClass and have it cover all the default scenarios. And I could still have flexibility to add new tests to cover very specific scenarios for each ViewModel, whenever needed.