EDennis.NetCoreTestingUtilities
1.0.0.1
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
<PackageReference Include="EDennis.NetCoreTestingUtilities" Version="1.0.0.1" />
paket add EDennis.NetCoreTestingUtilities --version 1.0.0.1
#r "nuget: EDennis.NetCoreTestingUtilities, 1.0.0.1"
// 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 | Versions 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. |
-
.NETCoreApp 2.0
- JsonDiffPatch (>= 2.0.45)
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,213 | 2/4/2020 |
3.8.5 | 719 | 2/3/2020 |
3.8.4 | 735 | 2/3/2020 |
3.8.3 | 943 | 2/3/2020 |
3.8.2 | 813 | 1/28/2020 |
3.8.1 | 651 | 1/22/2020 |
3.8.0 | 697 | 1/22/2020 |
3.7.9 | 852 | 1/11/2020 |
3.7.8 | 802 | 1/11/2020 |
3.7.7 | 729 | 1/9/2020 |
3.7.6 | 801 | 1/9/2020 |
3.7.5 | 786 | 1/9/2020 |
3.7.4 | 751 | 1/9/2020 |
3.7.3 | 710 | 1/9/2020 |
3.7.2 | 788 | 1/4/2020 |
3.7.1 | 726 | 1/2/2020 |
3.7.0 | 659 | 1/2/2020 |
3.6.7 | 775 | 12/19/2019 |
3.6.6 | 722 | 12/19/2019 |
3.6.5 | 761 | 12/18/2019 |
3.6.4 | 724 | 12/12/2019 |
3.6.3 | 703 | 12/12/2019 |
3.6.2 | 667 | 12/12/2019 |
3.6.1 | 713 | 12/10/2019 |
3.6.0 | 724 | 10/22/2019 |
3.5.0 | 712 | 10/8/2019 |
3.4.8 | 802 | 9/12/2019 |
3.4.7 | 783 | 9/10/2019 |
3.4.6 | 824 | 8/16/2019 |
3.4.5 | 813 | 6/21/2019 |
3.4.3 | 770 | 5/7/2019 |
3.4.2 | 779 | 5/7/2019 |
3.4.1 | 779 | 5/3/2019 |
3.4.0 | 6,251 | 3/25/2019 |
3.3.1 | 802 | 3/5/2019 |
3.3.0 | 806 | 3/5/2019 |
3.2.2 | 5,112 | 2/20/2019 |
3.2.1 | 813 | 2/8/2019 |
3.2.0 | 895 | 12/15/2018 |
3.1.0 | 1,570 | 11/19/2018 |
3.0.0 | 1,556 | 9/25/2018 |
2.9.9 | 1,503 | 9/24/2018 |
2.9.8 | 1,554 | 9/24/2018 |
2.9.7 | 1,762 | 9/21/2018 |
2.9.6 | 1,641 | 9/21/2018 |
2.9.5 | 1,805 | 9/21/2018 |
2.9.4 | 1,725 | 9/20/2018 |
2.9.3 | 1,738 | 9/18/2018 |
2.9.2 | 1,748 | 9/18/2018 |
2.9.1 | 1,638 | 9/17/2018 |
2.9.0 | 1,571 | 9/11/2018 |
2.8.4 | 1,013 | 8/16/2018 |
2.8.3 | 1,060 | 8/10/2018 |
2.8.2 | 1,080 | 8/9/2018 |
2.8.1 | 1,127 | 8/9/2018 |
2.8.0 | 1,115 | 8/9/2018 |
2.7.2 | 1,089 | 8/7/2018 |
2.7.1 | 1,100 | 8/7/2018 |
2.7.0 | 1,016 | 8/6/2018 |
2.6.0 | 1,069 | 8/2/2018 |
2.5.4 | 1,068 | 7/23/2018 |
2.5.3 | 1,050 | 7/23/2018 |
2.5.2 | 1,174 | 6/21/2018 |
2.5.1 | 1,088 | 6/15/2018 |
2.5.0 | 1,162 | 6/6/2018 |
2.4.1 | 1,093 | 6/4/2018 |
2.4.0 | 1,161 | 5/25/2018 |
2.3.9 | 1,219 | 5/2/2018 |
2.3.8 | 1,188 | 4/17/2018 |
2.3.7 | 1,332 | 4/6/2018 |
2.3.6 | 1,276 | 3/23/2018 |
2.3.5 | 1,166 | 3/9/2018 |
2.3.4 | 1,201 | 3/9/2018 |
2.3.3 | 1,067 | 3/8/2018 |
2.3.2 | 1,079 | 3/8/2018 |
2.3.1-alpha | 1,138 | 3/6/2018 |
2.2.4 | 1,206 | 3/3/2018 |
2.2.3.1 | 1,217 | 3/3/2018 |
2.2.3 | 1,182 | 3/3/2018 |
2.2.2 | 1,150 | 2/21/2018 |
2.2.1 | 1,158 | 2/21/2018 |
2.2.0 | 1,164 | 2/10/2018 |
2.1.1 | 1,102 | 2/8/2018 |
2.1.0.1 | 1,260 | 12/21/2017 |
2.1.0 | 1,302 | 12/21/2017 |
2.0.0 | 1,170 | 12/21/2017 |
1.0.0.1 | 1,237 | 12/3/2017 |
1.0.0 | 1,585 | 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).