Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v4.4.0 #74

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading