创建具有autofixture一个结构不引发公共构造错误 [英] Creating a struct with autofixture throws no public constructor error

查看:104
本文介绍了创建具有autofixture一个结构不引发公共构造错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个struct例如:

I have a struct e.g.:

public struct Foo
{
    public int Bar;
    public string Baz;
}

和想使用autofixture我的单元测试来创建它。

and would like to create it in my unit tests using autofixture. I tried using the following:

IFixture fixture = new Fixture();
var f = fixture.CreateAnonymous<Foo>();



但是,这将引发 AutoFixture无法创建一个实例错误。是否有可能自动创建一个autofixture结构?怎么能这样做?请注意,我的工作的遗留代码,因此需要处理的结构。 :)

But this throws an AutoFixture was unable to create an instance error. Is it possible to auto create a struct with autofixture? How could this be done? Please note, that I am working on legacy code and thus need to deal with structs. :)

编辑:

变更

IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());

IFixture fixture = new Fixture();



由于这是不相关的问题。

Since it wasn't relevant for the question.

推荐答案

这实质上是如何C#编译器将值类型的假象。虽然文档似乎另有表示,从的角度反映,美孚结构没有公共的构造函数。

This is essentially an artefact of how the C# compiler treats value types. While the documentation seems to indicate otherwise, from the perspective of Reflection, the Foo struct has no public constructors.

例如:如果执行该代码:

E.g. if you execute this code:

var ctors = typeof(Foo).GetConstructors();



结果是一个空数组。

the result is an empty array.

然而,这段代码编译:

var f = new Foo();



所以你可能会说,AutoFixture应该能够创建富的一个实例。

so you could argue that AutoFixture should be able to create an instance of Foo.

然而,在最后,可变结构是邪恶并应不惜一切代价避免。更好的选择是改变美孚实现这样:

However, in the end, mutable structs are evil and should be avoided at all cost. A better option is to change the Foo implementation to this:

public struct Foo
{
    public Foo(int bar, string baz)
    {
        this.Bar = bar;
        this.Baz = baz;
    }

    public readonly int Bar;
    public readonly string Baz;
}

如果你这样做,你不仅现在有一个(更多)正确。值类型,但AutoFixture也能创造没有进一步修改的一个实例。

If you do this, not only do you now have a (more) correct value type, but AutoFixture is also able to create an instance without further modification.

因此,这是的GOOS 范式,你应该的听你的测试的。如果他们出现摩擦,这可能是你的生产代码的反馈。在这种情况下,这正是你即将搬起石头砸自己的脚,因为该值类型的实现是有缺陷的反馈信息。

Thus, this is a pretty good example of the GOOS paradigm that you should listen to your tests. If they present friction, it might be feedback about your production code. In this case, this is exactly feedback that you're about to shoot yourself in the foot because the value type implementation is flawed.

P.S。即使你'修复'的富结构就像上文所述,是什么使它成为一个结构,而不是一类的呢?它还包含一个字符串(引用类型)场,所以尽管自己想要住在栈上的结构,字符串场仍然会指向堆数据。

P.S. Even if you 'fix' the Foo struct like outlined above, what's the point of making it a struct instead of a class? It still contains a string (reference types) field, so even though the struct itself is going to live on the stack, the string field is still going to point to data in the heap.

我添加这个问题作为一个可能的对于AutoFixture新功能。

I've added this issue as a possible new feature for AutoFixture.

这篇关于创建具有autofixture一个结构不引发公共构造错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆