Search code examples
c#.netnunit

How to combine conditions for multiple subproperties when using NUnit Assert?


I'm asserting the content of a resulting array and I want to make sure the contained object respects all my specifications. The object has two different nested objects.

I'm unable to assert the content of the nested objects in a single command.

This is what I got

Assert.That(sut.FutureInstances, Has.Exactly(1).InstanceOf<CourseInstanceSummary>().With
    .Property(nameof(CourseInstanceSummary.Id)).EqualTo(courseInstance.Id).And
    .Property(nameof(CourseInstanceSummary.StartDate)).EqualTo(courseInstance.StartDate).And
    .Property(nameof(CourseInstanceSummary.TrainingMode)).EqualTo(courseInstance.TrainingMode).And
    .Property(nameof(CourseInstanceSummary.TotalSeats)).EqualTo(courseInstance.TotalSeats).And
    .Property(nameof(CourseInstanceSummary.Participants)).EqualTo(courseInstance.Participants).And
    .Property(nameof(CourseInstanceSummary.Room)).With
        .Property(nameof(CourseInstanceSummary.RoomSummary.Id)).EqualTo(courseInstance.Room!.Id).And
        .Property(nameof(CourseInstanceSummary.RoomSummary.Name)).EqualTo(courseInstance.Room!.Name)
);

Assert.That(sut.FutureInstances, Has.Exactly(1).InstanceOf<CourseInstanceSummary>().With
    .Property(nameof(CourseInstanceSummary.Id)).EqualTo(courseInstance.Id).And
    .Property(nameof(CourseInstanceSummary.StartDate)).EqualTo(courseInstance.StartDate).And
    .Property(nameof(CourseInstanceSummary.TrainingMode)).EqualTo(courseInstance.TrainingMode).And
    .Property(nameof(CourseInstanceSummary.TotalSeats)).EqualTo(courseInstance.TotalSeats).And
    .Property(nameof(CourseInstanceSummary.Participants)).EqualTo(courseInstance.Participants).And
    .Property(nameof(CourseInstanceSummary.Site)).With
        .Property(nameof(CourseInstanceSummary.SiteItem.Id)).EqualTo(courseInstance.Site!.Id).And
        .Property(nameof(CourseInstanceSummary.SiteItem.Name)).EqualTo(courseInstance.Site!.Name)
);

The two asserts are looking at the same object, so it would be cool if I could unify them.


Solution

  • You can try

    NUnit.Framework.Constraints.Constraint roomConstraint = Has.Property(nameof(CourseInstanceSummary.Room)).With
        .Property(nameof(CourseInstanceSummary.RoomSummary.Id)).EqualTo(courseInstance.Room!.Id).And
        .Property(nameof(CourseInstanceSummary.RoomSummary.Name)).EqualTo(courseInstance.Room!.Name);
    
    NUnit.Framework.Constraints.Constraint siteConstraint = Has.Property(nameof(CourseInstanceSummary.Site)).With
        .Property(nameof(CourseInstanceSummary.SiteItem.Id)).EqualTo(courseInstance.Site!.Id).And
        .Property(nameof(CourseInstanceSummary.SiteItem.Name)).EqualTo(courseInstance.Site!.Name)
    

    then

    Assert.That(sut.FutureInstances, Has.Exactly(1).InstanceOf<CourseInstanceSummary>().With
    .Property(nameof(CourseInstanceSummary.Id)).EqualTo(courseInstance.Id).And
    .Property(nameof(CourseInstanceSummary.StartDate)).EqualTo(courseInstance.StartDate).And
    .Property(nameof(CourseInstanceSummary.TrainingMode)).EqualTo(courseInstance.TrainingMode).And
    .Property(nameof(CourseInstanceSummary.TotalSeats)).EqualTo(courseInstance.TotalSeats).And
    .Property(nameof(CourseInstanceSummary.Participants)).EqualTo(courseInstance.Participants).And
    .Matches(roomConstraint).And
    .Matches(siteConstraint)
    );