【发布时间】:2012-10-17 09:17:07
【问题描述】:
我有一个结构,例如:
public struct Foo
{
public int Bar;
public string Baz;
}
并想在我的单元测试中使用 autofixture 创建它。我尝试使用以下内容:
IFixture fixture = new Fixture();
var f = fixture.CreateAnonymous<Foo>();
但这会引发AutoFixture was unable to create an instance 错误。是否可以使用 autofixture 自动创建结构?怎么可能做到这一点?请注意,我正在处理遗留代码,因此需要处理结构。 :)
编辑:
改变了
IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());
到
IFixture fixture = new Fixture();
因为它与问题无关。
【问题讨论】:
-
如果你去掉 AutoMoq 定制,它有什么不同吗?
-
没什么区别。
-
酷 - 把它排除在外,@Peter Porfy 可以消除他的猜测......
-
通常情况下,您不需要
AutoMoqCustomization。 (Foo类型中没有可以使用 Moq 自动模拟的抽象类或接口。) -
@PeterPorfy 模拟(/mock 代理)位 - AF Core 只是大量反射,依赖很少。
AutoMoqCustomization所在的AutoFixture.AutoMoq扩展是唯一可以发挥作用的东西。 (并不是说这是一个错误的猜测或不合理的 - 只是如果 AMC 被取出它会在四肢上表明动态代理正在发挥作用)
标签: c# unit-testing autofixture