diff --git a/CHANGELOG.md b/CHANGELOG.md index 285d767..ced2b88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ Represents the **NuGet** versions. +## v4.4.0 +- *Enhancement:* Added `ExpectJson` and `ExpectJsonFromResource` to `IValueExpectations` to enable value comparison against the specified (expected) JSON. + ## v4.3.2 - *Fixed*: Added `TraceRequestComparisons` support to `MockHttpClient` to enable tracing for all requests. diff --git a/Common.targets b/Common.targets index d6dcedd..f8c1cf9 100644 --- a/Common.targets +++ b/Common.targets @@ -1,6 +1,6 @@  - 4.3.2 + 4.4.0 preview Avanade Avanade diff --git a/src/UnitTestEx/Expectations/ExpectationsExtensions.cs b/src/UnitTestEx/Expectations/ExpectationsExtensions.cs index 56ce988..51e9a0b 100644 --- a/src/UnitTestEx/Expectations/ExpectationsExtensions.cs +++ b/src/UnitTestEx/Expectations/ExpectationsExtensions.cs @@ -2,8 +2,10 @@ using Microsoft.Extensions.Logging; using System; +using System.Diagnostics.CodeAnalysis; using System.Net; using System.Net.Http; +using System.Reflection; using UnitTestEx.Json; namespace UnitTestEx.Expectations @@ -199,6 +201,39 @@ public static TSelf ExpectValue(this IValueExpectations + /// Expects that the resultant value will be equal (uses ) to the specified . + /// + /// The expectations . + /// The value . + /// The . + /// The JSON . + /// The JSON paths to ignore. + /// The instance to support fluent-style method-chaining. +#if NET7_0_OR_GREATER + public static TSelf ExpectJson(this IValueExpectations expectations, [StringSyntax(StringSyntaxAttribute.Json)] string json, params string[] pathsToIgnore) where TSelf : IValueExpectations +#else + public static TSelf ExpectJson(this IValueExpectations expectations, string json, params string[] pathsToIgnore) where TSelf : IValueExpectations +#endif + { + expectations.ExpectationsArranger.GetOrAdd(() => new ValueExpectations(expectations.ExpectationsArranger.Owner, (TSelf)expectations)).SetExpectJson(t => json); + expectations.ExpectationsArranger.PathsToIgnore.AddRange(pathsToIgnore); + return (TSelf)expectations; + } + + /// + /// Expects that the resultant value will be equal (uses ) to the JSON from the named embedded resource. + /// + /// The expectations . + /// The value . + /// The . + /// The embedded resource name (matches to the end of the fully qualifed resource name) that contains the expected value as serialized JSON. + /// The JSON paths to ignore. + /// The instance to support fluent-style method-chaining. + /// Uses to load the embedded resource within the . + public static TSelf ExpectJsonFromResource(this IValueExpectations expectations, string resourceName, params string[] pathsToIgnore) where TSelf : IValueExpectations + => ExpectJson(expectations, Resource.GetJson(resourceName, Assembly.GetCallingAssembly()), pathsToIgnore); + +#endregion } } \ No newline at end of file diff --git a/tests/UnitTestEx.NUnit.Test/PersonControllerTest.cs b/tests/UnitTestEx.NUnit.Test/PersonControllerTest.cs index 90882fa..1e0e51c 100644 --- a/tests/UnitTestEx.NUnit.Test/PersonControllerTest.cs +++ b/tests/UnitTestEx.NUnit.Test/PersonControllerTest.cs @@ -270,6 +270,16 @@ public void Http_Update_Http3() .Run(HttpMethod.Post, "Person/1", new Person { FirstName = "Bob", LastName = "Smith" }); } + [Test] + public void Http_Update_Http3_AsJson() + { + using var test = ApiTester.Create(); + test.Http() + .ExpectStatusCode(System.Net.HttpStatusCode.OK) + .ExpectJson("{ \"id\": 1, \"firstName\": \"Bob\", \"lastName\": \"Smith\" }") + .Run(HttpMethod.Post, "Person/1", new Person { FirstName = "Bob", LastName = "Smith" }); + } + [Test] public void Http_Update_Http4() {