加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何简化单元测试DDD值对象与AutoFixture相等

发布时间:2020-12-15 08:41:31 所属栏目:百科 来源:网络整理
导读:我目前正在使用xUnit,Moq和AutoFixture为我的Domain项目编写单元测试.我们来看看这个测试方法: [Theory]public void SameValueAs_OnOtherHasDifferentEmail_ReturnsFalse(){ Fixture fix = new Fixture(); var sut = fix.CreateAnonymousCoreAddress(); var
我目前正在使用xUnit,Moq和AutoFixture为我的Domain项目编写单元测试.我们来看看这个测试方法:
[Theory]
public void SameValueAs_OnOtherHasDifferentEmail_ReturnsFalse()
{
    Fixture fix = new Fixture();
    var sut = fix.CreateAnonymous<CoreAddress>();

    var other = new CoreAddress(
        sut.Firstname,sut.Lastname,sut.Company,sut.Street,sut.City,sut.ZIP,"other@email.com");

    Assert.False(sut.SameValueAs(other));
}

正如您所看到的,我正在测试类CoreAddress及其SameValueAs方法.为了测试每个可能的情况,我必须创建测试方法OnOtherHasDifferentFirstname,OnOtherHasDifferentLastname等.这个模式可以吗?关于AutoFixture的使用,我能以某种方式简化这个吗?

解决方法

这种方法与我自己做的很相似.鉴于几乎所有的自动化测试都围绕着一些实际结果等于某些预期结果的测试,我从未真正理解为什么人们不希望自己单元测试平等.

对于像上面这样的值对象,它很容易变得有点乏味,因为它导致许多测试非常相似.

你可以使用xUnit.net的一个hack是这样的:

[Theory]
[InlineData("other first name",null,null)]
[InlineData(null,"other last name","other company","other street","other city","other zip","other@email.com")]
public void EqualsIsFalseWhenAtLeastOneValueDiffers(
    string firstName,string lastName,string company,string street,string city,string zip,string email)
{
    Fixture fix = new Fixture();
    var sut = fix.CreateAnonymous<CoreAddress>();

    var other = new CoreAddress(
        firstName ?? sut.Firstname,lastName ?? sut.Lastname,company ?? sut.Company,street ?? sut.Street,city ?? sut.City,zip ?? sut.Zip,email ?? sut.Email);

    Assert.False(sut.Equals(other));
}

然而,虽然它很紧凑,但我并不太热衷于做这样的事情,因为测试本身的圈复杂度为8 – 大约7太多……只是输入值的轻微错误配置可能会破坏测试并最终产生误报或漏报.

另一方面,我们都是程序员,如果事情变得重复,我们该怎么办?

写一些代码来消除乏味.这基本上就是AutoFixture.Idioms项目的全部内容,虽然它目前没有像上面这样的封装测试,但未来它可能会得到一个…它是开源的,我们偶尔会接受拉取请求;)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读