How to Simplify Unit Testing of DDD Value Equality with AutoFixture Objects

I am currently writing Unit Tests for my Domain project using xUnit, Moq, and AutoFixture. Let's look at this testing method:

[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)); } 

As you can see, I am testing the CoreAddress class and its SameValueAs method. To check all possible cases, I would have to create OnOtherHasDifferentFirstname, OnOtherHasDifferentLastname, and so on. Is this template okay? Can I simplify this somehow with respect to using AutoFixture?

+6
source share
2 answers

This approach is very similar to what I would do myself. Given that almost all automated testing revolves around testing, that some actual result equals some expected result, I never understood why people don’t want the unit test of equality itself.

With a value object similar to the above, it can easily become a little tedious as it leads to many tests that are very similar.

One hack you can use with xUnit.net is this:

 [Theory] [InlineData("other first name", null, null, null, null, null, null)] [InlineData(null, "other last name", null, null, null, null, null)] [InlineData(null, null, "other company", null, null, null, null)] [InlineData(null, null, null, "other street", null, null, null)] [InlineData(null, null, null, null, "other city", null, null)] [InlineData(null, null, null, null, null, "other zip", null)] [InlineData(null, null, null, null, null, null, " 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)); } 

However, although it is compact, I am not too keen on this, since the test itself has a cyclic complexity of 8 - this is about 7 too much ... Only a slight incorrect configuration of the input values ​​can destroy the test and, in the end, produce false positives, or false negatives.

On the other hand, we are all programmers here, and if something repeats, what should we do?

Write a code to pick up boredom. This is mainly an AutoFixture.Idioms project, and as long as it does not have an encapsulated test like the one above, it can get one in the future ... it's open source, and we sometimes get download requests;)

+7
source

I would not worry about unit testing the value of objects, as far as I know. If you really want to do this, there are ways to abstract such unit tests and make them much less tedious to write: http://kennethxu.blogspot.fr/2009/11/unit-test-value-object.html

+2
source

Source: https://habr.com/ru/post/923365/


All Articles