【发布时间】:2015-03-05 11:32:26
【问题描述】:
我在测试中使用 AutoFixture 和 AutoMoqCustomization。
我有一个服务是被测系统的依赖项:
ISomeService
{
Task<IEnumerable<int>> Get();
}
我在被测系统内部调用它:
var collection = await _someService.Get(); // collection is empty
我不在乎集合中的内容,但我需要集合不为空。我是这样做的:
_fixture.Freeze<Mock<ISomeService>>()
.Setup(service => service.Get())
.Returns(Task.FromResult(_fixture.CreateMany<int>()));
看起来应该通过自定义来完成。我创建并注册了一个:
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
}
}
}
问题是它的 Create 方法从未被输入。有什么想法或现成的解决方案吗?
编辑
添加 GenericTypeSpecification 源
public class GenericTypeSpecification : IRequestSpecification
{
private readonly Type _type;
public GenericTypeSpecification(Type type)
{
_type = type;
}
public bool IsSatisfiedBy(object request)
{
var requestedType = request as Type;
return requestedType != null &&
requestedType.IsGenericType &&
requestedType.GetGenericTypeDefinition() == _type;
}
}
【问题讨论】:
-
什么是
GenericTypeSpecification? -
@MarkSeemann 抱歉,我以为它是库的一部分,但事实证明肯定是另一个团队成员创建了它。请查看编辑。
-
还没有看到任何修改...
-
@MarkSeemann 忘记点击“保存编辑”。
标签: c# unit-testing autofixture