saturdaymp - SaturdayMP.NConstraints 0.1.0-alpha0010

Custom constraints for NUnit.

PM> Install-Package SaturdayMP.NConstraints -Version 0.1.0-alpha0010 -Source https://www.myget.org/F/saturdaymp/api/v3/index.json

Copy to clipboard

> nuget.exe install SaturdayMP.NConstraints -Version 0.1.0-alpha0010 -Source https://www.myget.org/F/saturdaymp/api/v3/index.json

Copy to clipboard

> dotnet add package SaturdayMP.NConstraints --version 0.1.0-alpha0010 --source https://www.myget.org/F/saturdaymp/api/v3/index.json

Copy to clipboard
<PackageReference Include="SaturdayMP.NConstraints" Version="0.1.0-alpha0010" />
Copy to clipboard
source https://www.myget.org/F/saturdaymp/api/v3/index.json

nuget SaturdayMP.NConstraints  ~> 0.1.0-alpha0010
Copy to clipboard

> choco install SaturdayMP.NConstraints --version 0.1.0-alpha0010 --source https://www.myget.org/F/saturdaymp/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "saturdaymp" -SourceLocation "https://www.myget.org/F/saturdaymp/api/v2"
Install-Module -Name "SaturdayMP.NConstraints" -RequiredVersion "0.1.0-alpha0010" -Repository "saturdaymp" -AllowPreRelease
Copy to clipboard

GitHub release (with filter) Nuget CI/CD Release Notes GitHub Sponsors

NConstraints

Adds additional constraints to NUnit such as comparing the properties of two objects.

Installing

Install by adding the SaturdayMP.NContraints NuGet package:

dotnet add package SaturdayMP.NConstraints

You can find alternative install methods on the NuGet page.

NConstraints is compatible with .NET Standard 2.0 and NUnit 3. If you would like to use NConstraints on a older project please try v1.0.0 which is compatiable with .NET Standard 1.6.

If you want the live on the wild side you can find the alpha/beta NuGet packages on MyGet.

Please report issues with the installation by opening an issue or pull request. Feel free to ping me if you want to use NConstraints with an older versions of NUnit and/or .NET and I'll see what I can do.

Quickstart

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

Details

NConstratins extends the Is helper class which is used when writting Assert.That statements. 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>
///     Objects are equivalent if they have the same properties and they are all the same.
/// </summary>
[Test]
public void PropertiesTheSame()
{
    var expected = new TestClass() {IntegerPropery = 1, StringPropery = "Test"};
    var actual = new TestClass() {IntegerPropery = expected.IntegerPropery, StringPropery = expected.StringPropery};

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

/// <summary>
///     Objects are alos equivalent if all the property values match but actual has properties not on actual.
/// </summary>
[Test]
public void PropertiesTheSame()
{
    var expected = new TestClass() {IntegerPropery = 1, StringPropery = "Test"};
    var actual = new TestClass2() {IntegerPropery = expected.IntegerPropery, SecondIntegerProperty = 2, StringPropery = expected.StringPropery};

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

/// <summary>
///     Objects are NOT equivalent if expected has a property not on actual.
/// </summary>
[Test]
public void PropertyDoesNotExistOnActual()
{
    var expected = new TestClass2() { IntegerPropery = 1, SecondIntegerProperty = 2, StringPropery = "Test"};
    var actual = new TestClass() { IntegerPropery = expected.IntegerPropery, StringPropery = expected.StringPropery};

    Assert.That(expected, Is.Not.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. NUnit was one of the first frameworks as a young developer.

  • .NETStandard 1.6: 1.6.0.0

Owners

Christopher Cumming

Authors

SaturdayMP

Project URL

https://github.com/saturdaymp/NConstraints

License

MIT

Tags

nunit testing tdd unit-testing nunit-custom-constraints saturdaymp

Info

8 total downloads
1 downloads for version 0.1.0-alpha0010
Download (4.56 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
1.2.0-pullrequest0009-0009 8.05 KB Tue, 16 Jan 2024 22:15:48 GMT 1
1.2.0-pullrequest0009-0008 8.06 KB Tue, 16 Jan 2024 22:11:36 GMT 0
1.2.0-pullrequest0007-0006 7.81 KB Wed, 19 Jul 2023 21:10:29 GMT 0
1.2.0-pullrequest0006-0003 7.41 KB Wed, 19 Jul 2023 19:45:49 GMT 0
1.2.0-pullrequest0006-0002 7.39 KB Wed, 19 Jul 2023 16:46:40 GMT 0
1.2.0-alpha0009 8 KB Tue, 16 Jan 2024 22:18:31 GMT 0
1.2.0-alpha0006 7.75 KB Wed, 19 Jul 2023 21:25:17 GMT 0
1.1.1 7.73 KB Wed, 19 Jul 2023 21:09:18 GMT 1
1.1.1-beta0005 7.76 KB Wed, 19 Jul 2023 21:05:43 GMT 1
1.1.1-beta0003 7.36 KB Wed, 19 Jul 2023 20:22:00 GMT 0
1.1.0 7.32 KB Tue, 18 Jul 2023 22:21:40 GMT 0
1.1.0-pullrequest0005-0030 7.39 KB Tue, 18 Jul 2023 21:49:45 GMT 1
1.1.0-pullrequest0005-0028 7.4 KB Tue, 18 Jul 2023 21:43:41 GMT 0
1.1.0-pullrequest0005-0026 7.4 KB Tue, 18 Jul 2023 21:13:01 GMT 0
1.1.0-pullrequest0005-0025 7.39 KB Tue, 18 Jul 2023 21:08:53 GMT 0
1.1.0-pullrequest0005-0024 7.39 KB Tue, 18 Jul 2023 21:07:48 GMT 0
1.1.0-pullrequest0004-0022 7.4 KB Tue, 18 Jul 2023 17:33:20 GMT 0
1.1.0-pullrequest0004-0021 7.39 KB Sat, 15 Jul 2023 22:44:00 GMT 0
1.1.0-alpha0031 7.34 KB Tue, 18 Jul 2023 21:58:19 GMT 1
1.1.0-alpha0022 7.34 KB Tue, 18 Jul 2023 20:47:27 GMT 0
1.1.0-alpha0003 4.56 KB Thu, 26 Oct 2017 17:26:00 GMT 1
1.1.0-alpha0001 4.56 KB Mon, 16 Oct 2017 21:20:21 GMT 0
1.0.0 4.54 KB Mon, 16 Oct 2017 19:58:53 GMT 1
0.1.0-alpha0010 4.56 KB Fri, 13 Oct 2017 23:09:37 GMT 1
0.1.0-alpha0009 4.56 KB Fri, 13 Oct 2017 20:56:00 GMT 0
0.1.0-alpha0008 4.56 KB Fri, 13 Oct 2017 20:51:17 GMT 0