Pat
Pat

Reputation: 1275

Is there a better way to do these boiler plate tests in C#?

Currently using AutoFixture for the boiler plate code of guard methods. But is there a better way to do the rest of the boiler plate code in the unit tests? Or is there no need to test in the first place?

        fixture.Create<TestClass>();
        var testException = new ArgumentNullBehavior();
        var assertion = new GuardClauseAssertion(fixture, testException);
        assertion.Verify(typeof(TestClass).GetConstructors());

        //Boiler plate part below:
        var testClass = new TestClass(lang, refer, link, info, title, description);
        Assert.AreEqual(lang, testClass.Lang);
        Assert.AreEqual(refer, testClass.Refer);
        Assert.AreEqual(link, testClass.Link);
        Assert.AreEqual(info, testClass.Info);
        Assert.AreEqual(title, testClass.Title);
        Assert.AreEqual(description, testClass.Description);

Upvotes: 0

Views: 149

Answers (2)

holbizmetrics
holbizmetrics

Reputation: 101

You could also use the SemanticComparison NuGet package.

Assuming your class is defined as in the example below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoFixture;
using AutoFixture.Idioms;
using NUnit.Framework;
using SemanticComparison;
namespace LikenessProofOfConcept
{
    [TestFixture]
    public class TestClassTests
    {
        [Test]
        public void Test()
        {
            Fixture fixture = new Fixture();
            TestClass testClass1 = fixture.Create<TestClass>();
            TestClass testClass2 = fixture.Create<TestClass>();

             
            //var assertion = new GuardClauseAssertion(fixture, new EmptyStringBehaviorExpectation());
            //assertion.Verify(typeof(TestClass).GetConstructors());

            //Boiler plate part below:

            string lang = "en";
            string refer = "refer";
            string link = "link";
            string info = "info";
            string title = "title";
            string description = "description";
            TestClass deterministicTestClass1 = new TestClass(lang, refer, link, info, title, description);
            TestClass deterministicTestClass2 = new TestClass(lang, refer, link, info, title, description);
            TestClass deterministicTestClass3 = new TestClass(lang, refer, link, "Let's create another info to create a difference", title, description);

            // This should pass because the classes properties are all the same.
            Assert.That(new Likeness<TestClass, TestClass>(deterministicTestClass1).Equals(deterministicTestClass2), Is.True);

            // It should also be a fact that those differ. Thus checking for false.
            Assert.That(new Likeness<TestClass, TestClass>(deterministicTestClass1).Equals(deterministicTestClass3), Is.False);

            // This should pass because the classes OF the COMPARED properties are all the same.
            Assert.That(new Likeness<TestClass, TestClass>(deterministicTestClass1).Without(i => i.Info).Equals(deterministicTestClass3), Is.True);
        }
    }

    public class TestClass
    {
        public TestClass()
        {

        }

        public TestClass(string lang, string refer, string link, string info, string title, string description)
        {
            Lang = lang;
            Refer = refer;
            Link = Link;
            Info = info;
            Title = title;
            Description = description;

        }
        public string Lang { get; }
        public string Refer { get; }
        public string Link { get; }
        public string Info { get; }
        public string Title { get; }
        public string Description { get; }
    }
}

Now, semantic comparison can get rid of you to detect each property on its own. But instead compare all and you can customize. With methods/properties and so on of the Likeness and others that remind of the flexibility of AutoMoq.

P.S: And you may be even integrating your own extension methods to enable fluent syntax.

Upvotes: 0

Jaroslav
Jaroslav

Reputation: 326

I would recommend installing FluentAssertions package:
NuGet\Install-Package FluentAssertions -Version 6.9.0

Then you could replace those manual assertions using object equivalency method: actualTestClass.Should().BeEquivalentTo(expectedTestClass);

In general this package has various functionality that might save you from writing this boilerplate code you have posted. Ex.: checking if DateTime fields are close to certain value.

Here's the link for docs - https://fluentassertions.com/objectgraphs/

Upvotes: 1

Related Questions