为什么不使用AutoFixture Freeze,SemanticComparison Likeness和CreateProxy简单测试通过? [英] Why doesn't simple test pass using AutoFixture Freeze, SemanticComparison Likeness and CreateProxy?

查看:109
本文介绍了为什么不使用AutoFixture Freeze,SemanticComparison Likeness和CreateProxy简单测试通过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过两个简单类的实例来了解如何使用Likeness<T>()CreateProxy()功能.

I'm trying to understand how to use the CreateProxy() feature of Likeness<T>() using two instances of a simple class.

public class Band
{
    public string Strings { get; set; }
    public string Brass { get; set; }
}

在下面的测试中,我使用FixtureCreate<T>一个Band实例,并带有两个字符串属性的值.

With the following test, I use a Fixture to Create<T> a Band instance with values for the two string properties.

[Fact]
public void Equality_Behaves_As_Expected()
{
    // arrange
    var fixture = new Fixture();
    fixture.Customize(new AutoMoqCustomization());

    var original = fixture.Create<Band>();
    //   Brass something like --> "Brass65756b89-d9f3-42f8-88fc-ab6de5ae65cd"
    //   Strings something like --> "Strings7439fa1b-014d-4544-8428-baea66858940"

    // act
    var dupe = new Band {Brass = original.Brass, 
                         Strings = original.Strings};
    //   Brass same as original's like --> "Brass65756b89-d9f3-42f8-88fc-ab6de5ae65cd"
    //   Strings same as original's like --> "Strings7439fa1b-014d-4544-8428-baea66858940"

我尝试了许多不同的断言,但是问题的症结似乎是CreateProxy方法没有填充Band的属性,因此即使当我尝试比较Band的两个实例时具有相同的属性值,CreateProxy方法中的实例始终具有空值.

I've tried many different assertions, but the crux of the matter seems to be that the CreateProxy method is not populating the properties of Band, so that even when I try to compare two instances of Band with the same property values, the instance from the CreateProxy method always has null values.

    // assert
    var likeness = dupe.AsSource().OfLikeness<Band>()
                       .Without(x => x.Brass).CreateProxy();
    //   Brass & String properties are null using dupe as source of likeness (!)

    //var likeness = original.AsSource().OfLikeness<Band>()
    //                       .Without(x => x.Brass).CreateProxy();
    //   Brass & String properties are null using original as source of likeness (!)

    //Assert.True(likeness.Equals(original)); // Fails
    //Assert.True(original.Equals(likeness)); // Fails

    // below are using FluentAssertions assembly
    //likeness.Should().Be(original);           // Fails (null properties)
    //original.Should().Be(likeness);           // Fails (null properties)
    //likeness.ShouldBeEquivalentTo(original);  // Fails (null properties)
    //original.ShouldBeEquivalentTo(likeness);  // Fails (null properties)
}

我必须做错了什么,但是我已经阅读了在Ploeh博客和SO上可以找到的所有内容,并且找不到一个足够简单的示例来与我的工作进行比较.有什么想法吗?

I've gotta be doing something wrong, but I've read everything I can find on the Ploeh blog and SO, and can't find an example suitably simple enough to compare to what I'm doing. Any ideas?

推荐答案

如果在代理实例上分配值(调用CreateProxy方法之后),则测试通过:

If you assign the values on the proxied instance (after calling the CreateProxy method) the test passes:

[Fact]
public void Equality_Behaves_As_Expected()
{
    // AutoMoqCustomization is not necessary.
    var original = new Fixture().Create<Band>();

    var likeness = original
        .AsSource()
        .OfLikeness<Band>()
        .Without(x => x.Brass)
        .CreateProxy();

    likeness.Brass = "foo"; // Ignored.
    likeness.Strings = original.Strings;

    Assert.True(likeness.Equals(original));
    likeness.Should().Be(original);
    likeness.ShouldBeEquivalentTo(original);
}

请记住,Likeness target 类型上创建代理,并且只有该类型的实例会覆盖Equals.

Keep in mind that Likeness creates a proxy on the target type and only that type's instance overrides Equals.

由于 source 类型保持不变,因此以下断言将不会成功:

Since the source type remains intact, the following assertions will not succeed:

Assert.True(original.Equals(likeness));
original.Should().Be(likeness);
original.ShouldBeEquivalentTo(likeness);

Assert.True(original.Equals(likeness));
original.Should().Be(likeness);
original.ShouldBeEquivalentTo(likeness);

更新

从3.0.4版本开始,值会自动复制到代理实例(这意味着likeness.Strings = original.Strings;将自动发生).

From version 3.0.4 and above the values are automatically copied to the proxy instance (which means, likeness.Strings = original.Strings; is going to happen automatically).

这篇关于为什么不使用AutoFixture Freeze,SemanticComparison Likeness和CreateProxy简单测试通过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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