jeudi 5 mars 2015

I'm using AutoFixture with AutoMoqCustomization in my tests.


I have a service which is a dependency of the system under test:



ISomeService
{
Task<IEnumerable<int>> Get();
}


I call it inside the system under test:



var collection = await _someService.Get(); // collection is empty


I don't care what's inside the collection, but I need the collection not to be empty. I do it this way:



_fixture.Freeze<Mock<ISomeService>>()
.Setup(service => service.Get())
.Returns(Task.FromResult(_fixture.CreateMany<int>()));


It looks like it should be done with a customization. I created and registered one:



public class TaskCollectionCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(
new FilteringSpecimenBuilder(
new TaskCollectionBuilder(),
new GenericTypeSpecification(typeof(Task<>))));
}

private class TaskCollectionBuilder : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
// never enters here
}
}
}


The problem is it's Create method is never entered. Any ideas or ready-to-serve solutions?


Aucun commentaire:

Enregistrer un commentaire