vendredi 11 décembre 2015

Automate the testing of property assignments in constructor

In my domain layer I create a lot of classes which looks like this

public class Route
{
    public Route(Location origin, Location destination)
    {
        this.Origin = origin;
        this.Destination = destination;
    }
    public Location Origin { get; }
    public Location Destination { get; }
}

Now I need to unit-test this constructor

[Test]
public void PropertiesAreAssigned()
{
    var origin = new Location(...);
    var destination = new Location(...);
    var route = new Route(origin, destination);
    route.Origin.Should().Be(origin);
    route.Destination.Should().Be(destination);
}

I have quite a few of those classes and tests which are very similar to each other. What I'd like is to have some kind of method which just accepts the type arguments, and then does all the testing for me, i.e.

  • instantiate a variable per constructor parameter (with whole graph of dependencies needed)
  • pass these variables to constructor
  • checks that the property values are assigned correctly

I'm pretty sure I can do this with a bit of reflection, but maybe is there an existing library for doing that? More tests are welcome, e.g.

  • Testing the null argument throws the proper exception (I've already made a reflection-based test for that)
  • Testing that Equals and GetHashCode are implemented correctly




Aucun commentaire:

Enregistrer un commentaire