stupid question possibly, first time I'm actually writing a unit test at all (shame on me). I'm using Xunit and AutoFixture. So, I have some unit tests that I would like to run with different combinations of input data.
For example, say I'm testing my DAL and want to test the same unit test with different types of repositories (e.g. in-memory and SQL). I also need to have some other data created, which will be used both by the unit test as well as the repository. Some of my classes need to be created via factory methods, as they don't have any public constructors.
My ideal would be do to something like
[Theory, InMemoryRepository, SomeOtherData]
[Theory, SqlRepository, SomeOtherData]
// ... some other data combinations ...
public void SomeTest(IRepository repository, ISomeOtherData someOtherData)
{
// do some tests here
}
public class InMemoryRepositoryAttribute : AutoDataAttribute
{
public InMemoryRepositoryAttribute()
{
Fixture.Register<IRepository>(CreateRepository);
}
protected IRepository CreateRepository()
{
var data = Fixture.CreateMany<ISomeOtherData>().ToList(); // this will throw an exception "AutoFixture was unable to create an instance (...)" as my binding doesn't seem be available in the Fixture
var repository = new InMemoryDAL.Repository();
repository.Insert(data);
return repository;
}
}
public class SomeOtherDataAttribute : AutoDataAttribute
{
public SomeOtherDataAttribut()
{
Fixture.Register<ISomeOtherData>(CreateData);
}
protected ISomeOtherData CreateData()
{
string test = Fixture.Create<string>();
var data = (new SomeOtherDataFactory()).Create(test);
return data;
}
}
but this doesn't work, as both my AutoDataAttribute-classes seem to be based on seperate fixtures. I.e. whatever I register in my SomeOtherDataAttribute doesn't seem to be available in my InMemoryRepositoryAttribute instance.
Is there any way to fix this & use attributes to combine different sets of data in a Fixture?
Or what alternatives would you suggest - maybe best to create individual test functions for each data combination, and call SomeTest() explicitly from there?
Thanks!