Skip to content

Commit

Permalink
ExpectJson and ExpectJsonFromResource. (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
chullybun authored May 31, 2024
1 parent 4475458 commit 742f1cc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion Common.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>4.3.2</Version>
<Version>4.4.0</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Expand Down
37 changes: 36 additions & 1 deletion src/UnitTestEx/Expectations/ExpectationsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -199,6 +201,39 @@ public static TSelf ExpectValue<TSelf, TValue>(this IValueExpectations<TValue, T
return (TSelf)expectations;
}

#endregion
/// <summary>
/// Expects that the resultant value will be equal (uses <see cref="JsonElementComparer"/>) to the specified <paramref name="json"/>.
/// </summary>
/// <typeparam name="TSelf">The expectations <see cref="Type"/>.</typeparam>
/// <typeparam name="TValue">The value <see cref="Type"/>.</typeparam>
/// <param name="expectations">The <see cref="IValueExpectations{TValue, TSelf}"/>.</param>
/// <param name="json">The JSON <see cref="string"/>.</param>
/// <param name="pathsToIgnore">The JSON paths to ignore.</param>
/// <returns>The <typeparamref name="TSelf"/> instance to support fluent-style method-chaining.</returns>
#if NET7_0_OR_GREATER
public static TSelf ExpectJson<TSelf, TValue>(this IValueExpectations<TValue, TSelf> expectations, [StringSyntax(StringSyntaxAttribute.Json)] string json, params string[] pathsToIgnore) where TSelf : IValueExpectations<TValue, TSelf>
#else
public static TSelf ExpectJson<TSelf, TValue>(this IValueExpectations<TValue, TSelf> expectations, string json, params string[] pathsToIgnore) where TSelf : IValueExpectations<TValue, TSelf>
#endif
{
expectations.ExpectationsArranger.GetOrAdd(() => new ValueExpectations<TSelf>(expectations.ExpectationsArranger.Owner, (TSelf)expectations)).SetExpectJson(t => json);
expectations.ExpectationsArranger.PathsToIgnore.AddRange(pathsToIgnore);
return (TSelf)expectations;
}

/// <summary>
/// Expects that the resultant value will be equal (uses <see cref="JsonElementComparer"/>) to the JSON from the named embedded resource.
/// </summary>
/// <typeparam name="TSelf">The expectations <see cref="Type"/>.</typeparam>
/// <typeparam name="TValue">The value <see cref="Type"/>.</typeparam>
/// <param name="expectations">The <see cref="IValueExpectations{TValue, TSelf}"/>.</param>
/// <param name="resourceName">The embedded resource name (matches to the end of the fully qualifed resource name) that contains the expected value as serialized JSON.</param>
/// <param name="pathsToIgnore">The JSON paths to ignore.</param>
/// <returns>The <typeparamref name="TSelf"/> instance to support fluent-style method-chaining.</returns>
/// <remarks>Uses <see cref="Resource.GetJson(string, Assembly?)"/> to load the embedded resource within the <see cref="Assembly.GetCallingAssembly"/>.</remarks>
public static TSelf ExpectJsonFromResource<TSelf, TValue>(this IValueExpectations<TValue, TSelf> expectations, string resourceName, params string[] pathsToIgnore) where TSelf : IValueExpectations<TValue, TSelf>
=> ExpectJson(expectations, Resource.GetJson(resourceName, Assembly.GetCallingAssembly()), pathsToIgnore);

#endregion
}
}
10 changes: 10 additions & 0 deletions tests/UnitTestEx.NUnit.Test/PersonControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Startup>();
test.Http<Person>()
.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()
{
Expand Down

0 comments on commit 742f1cc

Please sign in to comment.