Skip to content

Commit

Permalink
Merge pull request #132 from adrianiftode/master-fluent-assertions-v8
Browse files Browse the repository at this point in the history
Add support for Fluent Assertions v8
  • Loading branch information
adrianiftode authored Jan 20, 2025
2 parents 990a202 + d3a6e41 commit 59b23af
Show file tree
Hide file tree
Showing 44 changed files with 925 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="FluentAssertions" Version="6.5.1" />
<PackageVersion Include="FluentAssertions" Version="[6.5.1,8.0.0)" />
<PackageVersion Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
Expand Down
28 changes: 28 additions & 0 deletions FluentAssertions.Web.sln
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Api.Net80", "samples
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Api.Net90", "samples\Sample.Api.Net90\Sample.Api.Net90.csproj", "{73E11379-908A-4BA0-9365-AE800A88C39B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentAssertions.Web.v8", "src\FluentAssertions.Web.v8\FluentAssertions.Web.v8.csproj", "{3872DF64-5DA9-4983-90D8-2802A0A26CFE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample.Api.v8.Tests", "test\Sample.Api.v8.Tests\Sample.Api.v8.Tests.csproj", "{EBB6D06F-B87F-4BDA-8F1B-A6E88C96C2E1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentAssertions.Web.v8.Tests", "test\FluentAssertions.Web.v8.Tests\FluentAssertions.Web.v8.Tests.csproj", "{056E517C-F644-4B8A-9494-09276E6E3C6B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentAssertions.Web.Types", "src\FluentAssertions.Web.Types\FluentAssertions.Web.Types.csproj", "{455E5F41-B678-4286-BB4D-FC232D08CAC0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -84,6 +92,22 @@ Global
{73E11379-908A-4BA0-9365-AE800A88C39B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73E11379-908A-4BA0-9365-AE800A88C39B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73E11379-908A-4BA0-9365-AE800A88C39B}.Release|Any CPU.Build.0 = Release|Any CPU
{3872DF64-5DA9-4983-90D8-2802A0A26CFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3872DF64-5DA9-4983-90D8-2802A0A26CFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3872DF64-5DA9-4983-90D8-2802A0A26CFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3872DF64-5DA9-4983-90D8-2802A0A26CFE}.Release|Any CPU.Build.0 = Release|Any CPU
{EBB6D06F-B87F-4BDA-8F1B-A6E88C96C2E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EBB6D06F-B87F-4BDA-8F1B-A6E88C96C2E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBB6D06F-B87F-4BDA-8F1B-A6E88C96C2E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBB6D06F-B87F-4BDA-8F1B-A6E88C96C2E1}.Release|Any CPU.Build.0 = Release|Any CPU
{056E517C-F644-4B8A-9494-09276E6E3C6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{056E517C-F644-4B8A-9494-09276E6E3C6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{056E517C-F644-4B8A-9494-09276E6E3C6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{056E517C-F644-4B8A-9494-09276E6E3C6B}.Release|Any CPU.Build.0 = Release|Any CPU
{455E5F41-B678-4286-BB4D-FC232D08CAC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{455E5F41-B678-4286-BB4D-FC232D08CAC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{455E5F41-B678-4286-BB4D-FC232D08CAC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{455E5F41-B678-4286-BB4D-FC232D08CAC0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -98,6 +122,10 @@ Global
{538479CE-5922-4FF9-A09D-80C9E91FFFF1} = {BFF7517A-C9EA-458D-829E-28A10F8D61BF}
{9B53847C-0D39-46AB-9E39-A2DCA0676D88} = {B6E9FBF3-AC48-48B0-9CBB-CA260F59CCD9}
{73E11379-908A-4BA0-9365-AE800A88C39B} = {B6E9FBF3-AC48-48B0-9CBB-CA260F59CCD9}
{3872DF64-5DA9-4983-90D8-2802A0A26CFE} = {14FDD52D-B83A-445B-BD2F-04B3E7B8033C}
{EBB6D06F-B87F-4BDA-8F1B-A6E88C96C2E1} = {BFF7517A-C9EA-458D-829E-28A10F8D61BF}
{056E517C-F644-4B8A-9494-09276E6E3C6B} = {BFF7517A-C9EA-458D-829E-28A10F8D61BF}
{455E5F41-B678-4286-BB4D-FC232D08CAC0} = {14FDD52D-B83A-445B-BD2F-04B3E7B8033C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {81F41C75-2F8A-4E70-BA17-38146C4BB6E6}
Expand Down
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ image: Visual Studio 2022
branches:
only:
- master
- master-v8
- /\d+\.\d+\.\d+/
pull_requests:
do_not_increment_build_number: false
Expand Down Expand Up @@ -70,7 +71,7 @@ artifacts:
deploy:
- provider: NuGet
api_key:
secure: aHT1IlqkfTAZm236Pkjt84NdjfEIrPJqJt4IwO5QNvH0LpIWqx/L3oFRewdCwCQs
secure: Ihowbb270n9ZDddCMfVAwAUNrEJ+Okjsld802+/Qw+cGxx3/ih8ToDxU8OxQ4sq9
skip_symbols: true
artifact: /.*\.nupkg/
on:
Expand Down
30 changes: 26 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ This is a [*FluentAssertions*](https://fluentassertions.com/) extension over the

It provides assertions specific to HTTP responses and outputs rich erros messages when the tests fail, so less time with debugging is spent.

[![Build status](https://ci.appveyor.com/api/projects/status/93qtbyftww0snl4x/branch/master?svg=true)](https://ci.appveyor.com/project/adrianiftode/fluentassertions-web/branch/master)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=adrianiftode_FluentAssertions.Web&metric=alert_status)](https://sonarcloud.io/project/overview?id=adrianiftode_FluentAssertions.Web)
[![NuGet](https://img.shields.io/nuget/v/FluentAssertions.Web.svg)](https://www.nuget.org/packages/FluentAssertions.Web)

```csharp
[Fact]
public async Task Post_ReturnsOk()
Expand Down Expand Up @@ -49,6 +45,26 @@ Once the response is ready you'll want to assert it. With first level properties

![FailedTest1](https://github.com/adrianiftode/FluentAssertions.Web/blob/master/docs/images/FailedTest1.png?raw=true)

### Status

[![Build status](https://ci.appveyor.com/api/projects/status/93qtbyftww0snl4x/branch/master?svg=true)](https://ci.appveyor.com/project/adrianiftode/fluentassertions-web/branch/master)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=adrianiftode_FluentAssertions.Web&metric=alert_status)](https://sonarcloud.io/project/overview?id=adrianiftode_FluentAssertions.Web)
[![NuGet](https://img.shields.io/nuget/v/FluentAssertions.Web.svg)](https://www.nuget.org/packages/FluentAssertions.Web)
[![NuGet FA v8](https://img.shields.io/nuget/v/FluentAssertions.Web.svg)](https://www.nuget.org/packages/FluentAssertions.Web.v8)

### Getting started

If you are using FluentAssertions < 8.0.0
```
dotnet add package FluentAssertions.Web
```

If you are using FluentAssertions >= 8.0.0

```
dotnet add package FluentAssertions.Web.v8
```

### FluentAssertions.Web Examples

- Asserting that the response content of a HTTP POST request is equivalent to a certain object
Expand Down Expand Up @@ -333,3 +349,9 @@ This library can still be used with FluentAssertions and it did not become obsol

When FluentAssertions.Web was created, [FluentAssertions.Http](https://github.com/balanikas/FluentAssertions.Http) also existed at the time, solving the same problem when considering the asserting language.
Besides the extra assertions added by FluentAssertions.Web, an important effort is put by this library on what happens when a test fails.

**FluentAssertions 8.0.0 and beyond**

Starting 8.0.0, FA is not an FOSS anymore. **FluentAssertions.Web** will maintain both FOSS (< 8.0.0) and the Commercial versions of FA (>= 8.0.0), so they will be deployed as separate Nuget packages:
- **FluentAssertions.Web** will continue to dependend on the FOSS versions
- **FluentAssertions.Web.v8** will dependend on the Commercial versions
4 changes: 0 additions & 4 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,4 @@
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluentAssertions.Web\FluentAssertions.Web.csproj" />
<ProjectReference Include="..\FluentAssertions.Web.Types\FluentAssertions.Web.Types.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ReSharper disable once CheckNamespace
using System;
using System.Runtime.Serialization;

namespace FluentAssertions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="System.Text.Json" />
</ItemGroup>
</Project>
5 changes: 5 additions & 0 deletions src/FluentAssertions.Web.Types/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
global using FluentAssertions.Web.Internal.Serializers;
global using System;
global using System.IO;
global using System.Text.Json;
global using System.Threading.Tasks;
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// ReSharper disable once CheckNamespace
using System;
using System.IO;
using System.Threading.Tasks;

namespace FluentAssertions;

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions src/FluentAssertions.Web.Types/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("FluentAssertions.Web.Tests")]
[assembly: InternalsVisibleTo("FluentAssertions.Web.v8.Tests")]
[assembly: InternalsVisibleTo("FluentAssertions.Web.FluentAssertionsWebConfig.Tests")]
31 changes: 31 additions & 0 deletions src/FluentAssertions.Web.v8/FluentAssertions.Web.v8.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageDescription>
This is a FluentAssertions extension over the HttpResponseMessage object. It provides assertions specific to HTTP responses and outputs rich erros messages when the tests fail, so less time with debugging is spent.
This version depends on the commercial version of the FluentAssertions library (&gt;= 8.0.0).
</PackageDescription>
</PropertyGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);FAV8</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" VersionOverride="8.0.0" />
<PackageReference Include="System.Text.Json" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluentAssertions.Web.Types\FluentAssertions.Web.Types.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\FluentAssertions.Web\**\*.*" Exclude="**\bin\**;**\obj\**;**\Properties\**;**\*.csproj">
<Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
<PackagePath>$(ContentTargetFolders)\src\%RecursiveDir%</PackagePath>
</Compile>
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions src/FluentAssertions.Web.v8/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("FluentAssertions.Web.v8.Tests")]
38 changes: 37 additions & 1 deletion src/FluentAssertions.Web/BadRequestAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ public class BadRequestAssertions : HttpResponseMessageAssertions
/// class.
/// </summary>
/// <param name="value">The subject value to be asserted.</param>
#if FAV8
/// <param name="assertionChain">The assertion chain to build and manage assertions.</param>
public BadRequestAssertions(HttpResponseMessage value, AssertionChain assertionChain) : base(value, assertionChain)
#else
public BadRequestAssertions(HttpResponseMessage value) : base(value)
#endif
{
}

Expand Down Expand Up @@ -55,8 +60,11 @@ public AndConstraint<BadRequestAssertions> HaveError(string expectedErrorField,
var fieldsOfErrorsProperty = json.GetChildrenNames(ErrorsPropertyName);
var allFields = json.GetChildrenNames("");
var fields = hasErrorsProperty ? fieldsOfErrorsProperty : allFields;

#if FAV8
CurrentAssertionChain
#else
Execute.Assertion
#endif
.BecauseOf(because, becauseArgs)
.ForCondition(fields.Any(field => string.Equals(field, expectedErrorField, StringComparison.OrdinalIgnoreCase)))
.FailWith("Expected {context:response} " +
Expand All @@ -72,7 +80,11 @@ public AndConstraint<BadRequestAssertions> HaveError(string expectedErrorField,
return !scope.Discard().Any();
});

#if FAV8
CurrentAssertionChain
#else
Execute.Assertion
#endif
.BecauseOf(because, becauseArgs)
.ForCondition(matchFound)
.FailWith("Expected {context:response} to contain " +
Expand Down Expand Up @@ -122,7 +134,11 @@ public AndConstraint<BadRequestAssertions> OnlyHaveError(string expectedErrorFie
var allFields = json.GetChildrenNames("");
var fields = hasErrorsProperty ? fieldsOfErrorsProperty : allFields;

#if FAV8
CurrentAssertionChain
#else
Execute.Assertion
#endif
.BecauseOf(because, becauseArgs)
.ForCondition(fields.Any(field => string.Equals(field, expectedErrorField, StringComparison.OrdinalIgnoreCase)))
.FailWith("Expected {context:response} " +
Expand All @@ -133,7 +149,11 @@ public AndConstraint<BadRequestAssertions> OnlyHaveError(string expectedErrorFie
var parent = hasErrorsProperty ? ErrorsPropertyName : json.GetParentKey(expectedErrorField);
var children = json.GetChildrenNames(parent);

#if FAV8
CurrentAssertionChain
#else
Execute.Assertion
#endif
.BecauseOf(because, becauseArgs)
.ForCondition(children.Count() == 1)
.FailWith("Expected {context:response} " +
Expand All @@ -149,7 +169,11 @@ public AndConstraint<BadRequestAssertions> OnlyHaveError(string expectedErrorFie
return !scope.Discard().Any();
});

#if FAV8
CurrentAssertionChain
#else
Execute.Assertion
#endif
.BecauseOf(because, becauseArgs)
.ForCondition(expectedWildcardErrorMessageMatchFound)
.FailWith("Expected {context:response} to contain " +
Expand All @@ -160,7 +184,11 @@ public AndConstraint<BadRequestAssertions> OnlyHaveError(string expectedErrorFie
expectedErrorField,
Subject);

#if FAV8
CurrentAssertionChain
#else
Execute.Assertion
#endif
.BecauseOf(because, becauseArgs)
.ForCondition(values.Count() == 1)
.FailWith("Expected {context:response} " +
Expand Down Expand Up @@ -202,7 +230,11 @@ public AndConstraint<BadRequestAssertions> NotHaveError(string expectedErrorFiel
var allFields = json.GetChildrenNames("");
var fields = hasErrorsProperty ? fieldsOfErrorsProperty : allFields;

#if FAV8
CurrentAssertionChain
#else
Execute.Assertion
#endif
.BecauseOf(because, becauseArgs)
.ForCondition(!fields.Any(c => string.Equals(c, expectedErrorField, StringComparison.OrdinalIgnoreCase)))
.FailWith("Expected {context:response} " +
Expand Down Expand Up @@ -247,7 +279,11 @@ public AndConstraint<BadRequestAssertions> HaveErrorMessage(string expectedWildc
return !scope.Discard().Any();
});

#if FAV8
CurrentAssertionChain
#else
Execute.Assertion
#endif
.BecauseOf(because, becauseArgs)
.ForCondition(matchFound)
.FailWith("Expected {context:response} to contain " +
Expand Down
6 changes: 4 additions & 2 deletions src/FluentAssertions.Web/BadRequestAssertionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ReSharper disable once CheckNamespace
#if !FAV8
// ReSharper disable once CheckNamespace
namespace FluentAssertions;

/// <summary>
Expand Down Expand Up @@ -108,4 +109,5 @@ public static AndConstraint<BadRequestAssertions> HaveErrorMessage(
string expectedWildcardErrorMessage,
string because = "", params object[] becauseArgs)
=> new BadRequestAssertions(parent.Subject).HaveErrorMessage(expectedWildcardErrorMessage, because, becauseArgs);
}
}
#endif
6 changes: 5 additions & 1 deletion src/FluentAssertions.Web/FluentAssertions.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" />
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluentAssertions.Web.Types\FluentAssertions.Web.Types.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 59b23af

Please sign in to comment.