我想创建一个自定义,将 AutoFixture 配置为传递类型,它确实具有关闭到 Moq 的实现。一般情况下我该如何做到这一点?
为了澄清,请考虑:
public class test
{
public string foo;
public test(IDictionary<string, string> stuffing)
{
foo = stuffing["bogus"];
}
}
[TestMethod]
public void testInjection()
{
var fixture = new Fixture();
bool didThrow;
try
{
var sut = fixture.Create<test>();
didThrow = false;
}
catch
{
didThrow = true;
}
Assert.IsTrue(didThrow);
}
测试通过。 AutoFixture 为我提供了一个与 IDictionary 兼容的虚拟集合。我希望测试失败...具体来说,我想将 IDictionary 交给 Moq 并获得默认情况下不会提示缺少键的东西。
我想使用类似 fixture.UseAutoMoq().ForceRelay<Dictionary<string,string>>()
的内容.
我也愿意接受更好的建议...但请考虑密封测试类。我正在测试经常使用此模式的代码,并且我想要围绕它进行测试约定。
最佳答案
要执行您要求的操作,您可以使用 AutoMoqCustomization
添加的 MockRelay
并将其移到 well-known-collections-builders 前面:
[Fact]
public void PassByMovingAutoMoqRelay()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var relay = fixture.ResidueCollectors.OfType<MockRelay>().Single();
fixture.Customizations.Add(relay);
var sut = fixture.Create<MyClass>(); // Doesn't throw
}
但是,我认为这不是一个好主意,因为大多数 .NET 集合接口(interface)都很庞大 LSP违规行为,并且不能保证自动模拟它们会产生有意义的行为 - 我希望情况相反。
关于c# - 强制中继到模拟框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28870254/