SaturdayMP.NConstraints 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SaturdayMP.NConstraints --version 1.1.0
NuGet\Install-Package SaturdayMP.NConstraints -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SaturdayMP.NConstraints" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SaturdayMP.NConstraints --version 1.1.0
#r "nuget: SaturdayMP.NConstraints, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SaturdayMP.NConstraints as a Cake Addin
#addin nuget:?package=SaturdayMP.NConstraints&version=1.1.0

// Install SaturdayMP.NConstraints as a Cake Tool
#tool nuget:?package=SaturdayMP.NConstraints&version=1.1.0

NConstraints

Adds additional constraints to NUnit such as comparing all the property values on two objects.

Installing

You can find the latest stable NuGet package at here. If you want the live on the wild side you can find the developer NuGet packages on MyGet.

If you have any issues with the installation please let me know by opening an issue or pull request.

Quickstart

The additional constraints can be used when using Assert.That and extend the Is helper class. For example:

Assert.That(expected, SaturdayMP.NConstraints.Is.EquivalentPropertyWiseTo(actual);

If you don't want to prifix the additional constratins with the SaturdayMP.NConstraints namespace then add the following to the top your test file:

using NUnit.Framework; // Assume you already added this
using SaturdayMP.NConstraints;  // Add this statement.
using Is = SaturdayMP.NConstraints.Is; // Add this statement.

Now you can write:

Assert.That(expected, Is.EquivalentPropertyWiseTo(actual);

The complete file of this example looks like:

using NUnit.Framework;
using SaturdayMP.NConstraints;
using Is = SaturdayMP.NConstraints.Is;

namespace MyTestNamespace
{
  [TestFixture]
  public class MyTests
  {
    [Test]
    public void PropertiesAreTheSame()
    {
      var expected = new TestClass() { IntegerProperty = 1 };
      var actual = new TestClass() {IntegerProperty = expected.IntegerProperty};

      Assert.That(expected, Is.EquivalentPropertyWiseTo(actual));
    }
  }
}

Notice that the using statement set the Is object. If you try:

using NUnit.Framework; 
using SaturdayMP.NConstraints;

You will get compile errors because there are two Is classes, one in the NUnit.Framework namespace and one in SaturdayMP.NConstraints.

Finally some code analyizers, like ReSharper, will raise warnings like "Access to a static member or a type via a dirvied type". You can safely ignore these warnings, I don't know a good way to remove these warnings. If you do please open an issue or pull request.

Constraints

This project adds the following constraints to NUnit:

Constraint Name Description
EquivalentPropertyWiseTo Asserts that the property values of expected object are the same on the actual object.

EquivalentPropertyWiseTo

Asserts that the property values of expected object are the same on the actual object. The objects don't have to be the same class but if a property exists on the expected object but not on the actual object then the assert fails.

/// <summary>
///     Properties don't match up.
/// </summary>
[Test]
public void PropertiesNotTheSame()
{
  var expected = new TestClass() {IntegerProperty = 1};
  var actual = new TestClass();

  Assert.That(expected, NUnit.Framework.Is.Not.EquivalentPropertyWiseTo(actual));
}

/// <summary>
///     Properties match.
/// </summary>
[Test]
public void PropertiesTheSame()
{
  var expected = new TestClass() { IntegerProperty = 1 };
  var actual = new TestClass() {IntegerProperty = expected.IntegerProperty};

  Assert.That(expected, Is.EquivalentPropertyWiseTo(actual));
}

Contributing

If you have any questions, notice a bug, or have a suggestion/enhancment please let me know by:

Acknowledgements

Thanks to the NUnit team for creating NUnit and continuing to support it. I’ve used NUnit for most of my career to properly test and improve my code.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1 151 7/19/2023
1.1.0 148 7/18/2023
1.0.0 1,011 10/16/2017