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

我目前正在使用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项目的全部内容,虽然它目前没有像上面这样的封装测试,但未来它可能会得到一个…它是开源的,我们偶尔会接受拉取请求;)

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么