Questions tagged [fluent-assertions]

Fluent Assertions is a set of .NET extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style test and which tries to keep you out of the debugger hell. Targets .NET Framework 4.5 and 4.7, as well as .NET Core 2.0, .NET Core 3.0, .NET Standard 1.3, 1.6 and 2.0. Supports the unit test frameworks MSTest, MSTest2, Gallio, NUnit, XUnit, MBUnit, MSpec, and NSpec.

Fluent Assertions is a library with extension methods to be used for assertions in your unit tests. The main goals of Fluent Assertions are to keep your unit tests readable, and to give descriptive error messages in case of failing tests.

Resources

394 questions
0
votes
1 answer

FluentAssertions - "nested" Exclude

I have an API providing tons of functionality, thatswhy there are massive abstractions to generics. Please imagine (with the code below) following scenario: Every Dto has an Id. I want to test against a Service, create the items. The service doenst…
0
votes
2 answers

How to exclude a JSON property from comparison

How to exclude a JSON property from comparison in FluentAssertions.Json? JToken token, expectedJson; token.Should().BeEquivalentTo(expectedJson); { "property1":"value1", "property2":"value2", "property3":"value3" } I want to exclude property…
Andrej B.
  • 187
  • 1
  • 5
  • 14
0
votes
0 answers

How do I mock Func with NSubstitute?

I have the following xUnit test. How should I do in order to mock Func with new ClientWebSocket() using NSubstitute? Btw, in addition ClientFactory doesn't want to 100% unit test coverage. public sealed class WebSocketOptions { …
nop
  • 4,711
  • 6
  • 32
  • 93
0
votes
1 answer

IEnumerable swallows an exception and a unit test cannot catch it

I'm unit testing the following method and I expect ArgumentOutOfRangeException to be thrown. Instead gridTrading.GridLevels().As() returns null for whatever reason. It basically swallows the exception. How do I fix it by keeping the return…
nop
  • 4,711
  • 6
  • 32
  • 93
0
votes
1 answer

Collect assertion failures using Fluent assertions in C#

My goal is to use fluent assertions to collect all assertion failures and report them. Currently, I use private static void AssertValue(object? actual, object? expected, string fieldName, IDictionary assertionFailures) { try …
Patrik Mihalčin
  • 3,341
  • 7
  • 33
  • 68
0
votes
0 answers

A list of products whose description contains any of the keywords in a string array to FluentAssertions

I wanted to wrap the LINQ query in the AllSatisfy condition. How do I make it filter the products containing any of the keywords? I did it and it works, but I wonder how to wrap it in the AllSatisfy condition. Should probably be smt similar to the…
nop
  • 4,711
  • 6
  • 32
  • 93
0
votes
1 answer

FluentAssertions alternative of TrueForAll

I'm trying to find an alternative of .TrueForAll using FluentAssertions. Note that in the Shoudly example Products is List whereas in the new code it is IEnumerable. It says A constant value is expected for minPrice and…
nop
  • 4,711
  • 6
  • 32
  • 93
0
votes
1 answer

Regex.IsMatch to MatchRegex in FluentAssertions

I looked at the documentation and there were methods e.g.Match, MatchRegex, etc., so I decided to improve the commented statement below and replace Regex.IsMatch with MatchRegex. However, I'm getting a compile-time error. How do I fix it? It's a…
nop
  • 4,711
  • 6
  • 32
  • 93
0
votes
0 answers

Is it possible to use TestCases in FluidAssertions to combine tests that check against different unique exception types?

I am trying to simplify my unit test code by utilizing [TestCase] instead of multiple [Test] methods. The functioning, bulky example of an implementation is as follows: [Test] public void Uri_Cannot_Be_Resolved_By_Not_Passing_A_Valid_Uri() { …
0
votes
1 answer

How to check the order of a nested collection with FluentAssertions

I am trying to check the order of a nested collection with FluentAssertion as follows. selectedStudent.MarkSheets.Select(x => x.Comments.Should().BeInDescendingOrder(x => x.CommentedTime)); But this seems to be not correct since even if I check…
xcxc0
  • 99
  • 1
  • 9
0
votes
1 answer

How do I express these tests correctly with fluent assertions?

I have the record type below. public record CustomFormatGroupItem(string GroupName, string CfName, string CfAnchor); I have a method that returns a collection of these, followed by two test assertions: ICollection result =…
void.pointer
  • 24,859
  • 31
  • 132
  • 243
0
votes
1 answer

Checking throws exception in IAsyncEnumerable method - xUnit

I have an async method lik this: public async IAsyncEnumerable> Load() { //some times throws exception in this line var responseEnumerable = _crawler.Crawl(); await foreach (var response in…
Hamidreza Samadi
  • 637
  • 1
  • 7
  • 24
0
votes
2 answers

Fluent assertion, ThenBeInAscendingOrder propertyExpression

I can't make it work, any help will be much apreciated. There is a list of entities from query response where I would like to test if they are in particular order, and I wish to make the comparison with Fluent assertion library but I have been…
alexDuty
  • 72
  • 1
  • 8
0
votes
1 answer

How to insert a message at the first line if there is a failure in an AssertionScope in FluentAssertions?

I'm looping through an array of results and comparing each object to the array of expected results resultItem.Should().BeEquivalentTo(expectedResultItem). Each item have a unique identifier and want to display it above. Here is an example result and…
eSPiYa
  • 882
  • 1
  • 12
  • 29
0
votes
2 answers

FluentAssertions : Should().BeEquivalentTo fails for un-ordered arrays of enums

I have a test in C# in which I use FluentAssertions to check the result. [Fact] public void GetEnabledFeaturesOK() { LTAddOnsType[] res = new LTAddOnsType[2]; res [0] = LTAddOnsType.Pro; res [1] = LTAddOnsType.Live; …
Cristiano Ghersi
  • 1,944
  • 1
  • 20
  • 46