FluentAssertions应该BeEquivalentTo()与Should().BeEquivalentTo() [英] FluentAssertions ShouldBeEquivalentTo() versus Should().BeEquivalentTo()

查看:162
本文介绍了FluentAssertions应该BeEquivalentTo()与Should().BeEquivalentTo()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个验证方法的集合输出的测试.测试的这种变化通过了:

I have a test that verifies the collection output of a method. This variation of the test passes:

    [TestMethod, TestCategory("BVT")]
    public void TheStatusesAreReturned()
    {
        var expectedUnprocessedStatuses = new List<FileUploadStatus>
            {
                FileUploadStatus.InProcess,
                FileUploadStatus.Pending,
            };

        Sut.GetUnprocessedStatuses()
            .Should()
            .BeEquivalentTo(expectedUnprocessedStatuses);
    }

此测试的变体失败,错误为预期项目[0]尚在处理中,但发现待处理":

This variation of the test fails, with the error "Expected item[0] to be InProcess, but found Pending":

    [TestMethod, TestCategory("BVT")]
    public void TheStatusesAreReturned2()
    {
        var expectedUnprocessedStatuses = new List<FileUploadStatus>
            {
                FileUploadStatus.InProcess,
                FileUploadStatus.Pending,
            };

        Sut.GetUnprocessedStatuses()
            .ShouldBeEquivalentTo(expectedUnprocessedStatuses);
    }

很显然,ShouldBeEquivalentTo在乎收集项的顺序,而BeEquivalentTo不在乎.为什么这两种方法之间的等效概念不同?

Clearly, ShouldBeEquivalentTo cares about collection item order, whereas BeEquivalentTo does not. Why is the notion of equivalency different between the 2 methods?

推荐答案

您是正确的. Should().BeEquivalentTo()正在使用各个项目的Equals()实现来验证其等效性,并且自版本1开始就存在.FA2.0中引入的更新的ShouldBeEquivalentTo()正在进行深入的结构比较,并报告任何差异.对于2.1,我将默认情况下将其行为更改为更像集合等效性

You're correct. Should().BeEquivalentTo() is using the individual items Equals() implementation to verify equivalence and has been around since version 1. The newer ShouldBeEquivalentTo() introduced in FA 2.0 is doing an in-depth structural comparison and also reporting on any differences. For 2.1 I'm going to change the behavior to be more like the collection equivalency by default

这篇关于FluentAssertions应该BeEquivalentTo()与Should().BeEquivalentTo()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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