EDennis.NetCoreTestingUtilities 1.0.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package EDennis.NetCoreTestingUtilities --version 1.0.0.1
NuGet\Install-Package EDennis.NetCoreTestingUtilities -Version 1.0.0.1
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="EDennis.NetCoreTestingUtilities" Version="1.0.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EDennis.NetCoreTestingUtilities --version 1.0.0.1
#r "nuget: EDennis.NetCoreTestingUtilities, 1.0.0.1"
#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 EDennis.NetCoreTestingUtilities as a Cake Addin
#addin nuget:?package=EDennis.NetCoreTestingUtilities&version=1.0.0.1

// Install EDennis.NetCoreTestingUtilities as a Cake Tool
#tool nuget:?package=EDennis.NetCoreTestingUtilities&version=1.0.0.1

Overview

This package utilizes the Newtonsoft JSON library to provide methods that make testing of objects and object collections a little easier. The package is intended to be used in Test projects only, as it uses reflection extensively and is not performance-optimized.

Object Extension Methods

The following extension methods for objects are provided:

bool IsSame(T obj)

This method returns true if the provided object variable and the current object variable (this) refer to the same object in memory. The method compares hashes and property values. Example:

[Test]
public void TestSameObjectInMemory() {
    Person p1 = new Person("Bob", "Jones");
    Person p2 = p1; //assign p2 to point to p1 (same object in memory)
    Assert.True(p1.IsSame(p2)); 
}

bool IsEqual(T obj)

This method returns true if the provided object variable and the current object variable (this) have the same property values. NOTE: this is a deep comparison. Example:

[Test]
public void TestEqualObjects() {
    Person p1 = new Person("Bob", "Jones");
    Person p2 = new Person("Bob", "Jones");
    Assert.False(p1.IsSame(p2));
    Assert.True(p1.IsEqual(p2));
}

bool IsEqual(T obj, string[] pathsToIgnore)

This method returns true if the provided object variable and the current object variable (this) have the same property values, ignoring all properties at the provided JSON paths. Example:

[Test]
public void TestEqualObjectsIgnoringSelectedProperties() {
    Person p1 = new Person(1, "Bob", "Jones");
    Person p2 = new Person(2, "Bob", "Jones");
    Assert.False(p1.IsEqual(p2));
    Assert.True(p1.IsEqual(p2, new string[] { "ID" })); //ignore ID
}

string ToJsonString()

This method serializes an object to a JSON string. Example:

[Test]
public void TestToJsonString() {
    string expectedJson =
        @"{
         ""ID"": 0,
         ""FirstName"": ""David"",
         ""LastName"": ""Parks""
        }";
    expectedJson = JToken.Parse(expectedJson).ToString(Formatting.Indented);
    Person p1 = new Person("David", "Parks");
    string actualJson = p1.ToJsonString();
    Assert.AreEqual(expectedJson, actualJson);
}

T FromJsonString<T>(string json)

This method creates a new object of type T by deserializing the provided JSON string. When called from an object constructor, this method provides a convenient way to load an object with properties. Example:

[Test]
public void TestFromJsonString() {
    string json =
        @"{
         ""FirstName"": ""David"",
         ""LastName"": ""Parks""
        }";
    var p1 = new Person().FromJsonString(json);
    Person p2 = new Person("David", "Parks");
    Assert.True(p1.IsEqual(p2));
}

T FromJsonPath<T>(JToken jtoken, string jsonPath)

This method creates a new object of type T by deserializing a JSON object or array at the provided path in the provided JToken object. When called from an object constructor, this method provides a convenient way to load an object with properties. Example:

[Test]
public void TestFromJsonPath1() {
    string json =
       @"{
         ""FirstName"": ""David"",
         ""LastName"": ""Parks"",
         ""Contact"": {
             ""FirstName"": ""Jill"",
             ""LastName"": ""Parks""
            }
        }";
    var jtoken = JToken.Parse(json);
    var p1 = new Person().FromJsonPath(jtoken, "Contact");
    Person p2 = new Person("Jill", "Parks");
    Assert.True(p1.IsEqual(p2));
}

T FromJsonPath<T>(string filePath, string objectPath)

This method creates a new object of type T by deserializing a JSON object or array in the provided JSON file at the provided path. When called from an object constructor, this method provides a convenient way to load an object with properties. NOTE: you can use "" or "/" instead of "." in the objectPath. Example:

[Test]
public void TestFromJsonPath2() {
    var p1 = new Person().FromJsonPath(@"DavidParks.json", "Contact");
    Person p2 = new Person("Jill", "Parks");
    Assert.True(p1.IsEqual(p2));
}

T FromJsonPath<T>(string jsonFileObjectPath)

This method is equivalent to the above method, but it combines the filePath and objectPath into a single expression. Note that you can use a backslash or forward slash instead of period in the objectPath. Example:

[Test]
public void TestFromJsonPath3() {
    var p1 = new Person().FromJsonPath(@"DavidParks.json\Contact");
    Person p2 = new Person("Jill", "Parks");
    Assert.True(p1.IsEqual(p2));
}

JsonAssert Class

A JsonAssert Class is provided with an static, overloaded ExecuteTest method that reads in a test case from a JSON file in a specific test folder/subfolder and executes the test case. This method makes numerous assumptions about the format of the JSON file and its location. When these assumptions are met, the C# code needed to invoke the method from a test class is minimal. Here are the assumptions:

  • The method under test is an instance method, not a static method.
  • The JSON file consists of a single JSON object whose property names (except for one) exactly match the parameter names for the method under test.
  • The expected return value for the test case is represented by a "returns" property in the JSON file.
  • All data within the JSON file reflect properly serialized values, which are parseable by Json.NET.

static void ExecuteTest<T>(T obj, string testCase)

This method accepts an instantiated object (obj) and a file name excluding folder and extension(testCase) as parameters. It throws a JsonAssertException if the test case fails. This method makes some additional assumptions:

  • The JSON test case must be placed in a subfolder whose name is the same as the method to be tested.
  • The method subfolder must be placed in a top-level folder whose name is the same as the name of the class to be tested.
  • The JSON file has a .json extension.

Example:

{
  "persons": [
    {
      "FirstName": "David",
      "LastName": "Parks",
      "DateOfBirth": "1987-06-29"
    },
    {
      "FirstName": "Johnny",
      "LastName": "Sandoval",
      "DateOfBirth": "2014-12-06"
    }
  ],
  "returns":     {
      "FirstName": "Johnny",
      "LastName": "Sandoval",
      "DateOfBirth": "2014-12-06"
    }
}
/// <summary>
/// Assumes the PersonManager class has an instance method
/// called Youngest and that for each of the provided testCase 
/// values in the range (1-2), there is a properly formatted
/// JSON file residing in the right folder and subfolder
/// (e.g., "PersonManager\Youngest\01.json")
/// </summary>
/// <param name="testCase">The name of the test case</param>
[Test]
public void Youngest([Range(1, 2)] int testCase) {
    JsonAssert.ExecuteTest(new PersonManager(), testCase.ToString("D2"));
}

static void ExecuteTest<T>(T obj, string filePath, string methodName, string testCase)

This method is the same as the previous method, but it allows the caller to explicitly set the file path and the name of the method under test.

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 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on EDennis.NetCoreTestingUtilities:

Package Downloads
EDennis.AspNetCore.Base

Alpha version that still requires (a) updated code comments, (b) updated wiki documentation, and (c) completed sample solution

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.8.6 1,118 2/4/2020
3.8.5 685 2/3/2020
3.8.4 691 2/3/2020
3.8.3 909 2/3/2020
3.8.2 770 1/28/2020
3.8.1 626 1/22/2020
3.8.0 661 1/22/2020
3.7.9 822 1/11/2020
3.7.8 760 1/11/2020
3.7.7 715 1/9/2020
3.7.6 768 1/9/2020
3.7.5 772 1/9/2020
3.7.4 721 1/9/2020
3.7.3 690 1/9/2020
3.7.2 764 1/4/2020
3.7.1 694 1/2/2020
3.7.0 639 1/2/2020
3.6.7 750 12/19/2019
3.6.6 687 12/19/2019
3.6.5 726 12/18/2019
3.6.4 710 12/12/2019
3.6.3 656 12/12/2019
3.6.2 635 12/12/2019
3.6.1 692 12/10/2019
3.6.0 682 10/22/2019
3.5.0 680 10/8/2019
3.4.8 775 9/12/2019
3.4.7 740 9/10/2019
3.4.6 811 8/16/2019
3.4.5 766 6/21/2019
3.4.3 739 5/7/2019
3.4.2 768 5/7/2019
3.4.1 738 5/3/2019
3.4.0 6,170 3/25/2019
3.3.1 772 3/5/2019
3.3.0 785 3/5/2019
3.2.2 5,006 2/20/2019
3.2.1 787 2/8/2019
3.2.0 870 12/15/2018
3.1.0 1,526 11/19/2018
3.0.0 1,516 9/25/2018
2.9.9 1,464 9/24/2018
2.9.8 1,534 9/24/2018
2.9.7 1,726 9/21/2018
2.9.6 1,598 9/21/2018
2.9.5 1,776 9/21/2018
2.9.4 1,676 9/20/2018
2.9.3 1,708 9/18/2018
2.9.2 1,710 9/18/2018
2.9.1 1,596 9/17/2018
2.9.0 1,536 9/11/2018
2.8.4 974 8/16/2018
2.8.3 1,012 8/10/2018
2.8.2 1,050 8/9/2018
2.8.1 1,087 8/9/2018
2.8.0 1,066 8/9/2018
2.7.2 1,049 8/7/2018
2.7.1 1,072 8/7/2018
2.7.0 1,004 8/6/2018
2.6.0 1,041 8/2/2018
2.5.4 1,056 7/23/2018
2.5.3 1,017 7/23/2018
2.5.2 1,150 6/21/2018
2.5.1 1,059 6/15/2018
2.5.0 1,125 6/6/2018
2.4.1 1,058 6/4/2018
2.4.0 1,120 5/25/2018
2.3.9 1,183 5/2/2018
2.3.8 1,156 4/17/2018
2.3.7 1,307 4/6/2018
2.3.6 1,245 3/23/2018
2.3.5 1,143 3/9/2018
2.3.4 1,141 3/9/2018
2.3.3 1,045 3/8/2018
2.3.2 1,067 3/8/2018
2.3.1-alpha 1,108 3/6/2018
2.2.4 1,181 3/3/2018
2.2.3.1 1,185 3/3/2018
2.2.3 1,164 3/3/2018
2.2.2 1,120 2/21/2018
2.2.1 1,135 2/21/2018
2.2.0 1,132 2/10/2018
2.1.1 1,064 2/8/2018
2.1.0.1 1,229 12/21/2017
2.1.0 1,262 12/21/2017
2.0.0 1,146 12/21/2017
1.0.0.1 1,204 12/3/2017
1.0.0 1,554 11/30/2017

Modified FromJsonPath to allow both forward slash and backward slash in the objectPath parameter.  Fixed minor bug with FromJsonString (previously, it required no-arg constructor).