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
18
votes
1 answer

Fluent Assertions: Approximately compare a classes properties

I have a class Vector3D that has the properties X, Y and Z of type double (it also has other properties such as Magnitude). What is the best way of approximately comparing all the properties or a selection of the properties at a given precision…
Ayb4btu
  • 3,208
  • 5
  • 30
  • 42
18
votes
3 answers

C# Fluent Assertions global options for ShouldBeEquivalentTo

In Fluent Assertions when comparing objects with DateTime properties there are sometimes a slight mismatch in the milliseconds and the comparison fail. The way we get around it is to set the comparison option like…
Hawk
  • 692
  • 2
  • 7
  • 18
17
votes
3 answers

Fluent assertions: Assert one OR another value

Using fluent assertions, I would like to assert that a given string contains either one of two strings: actual.Should().Contain("oneWay").Or().Should().Contain("anotherWay"); // eiter value should pass the assertion. // for example: "you may do it…
Marcel
  • 15,039
  • 20
  • 92
  • 150
17
votes
3 answers

Fluent Assertions: Using BeCloseTo on a collection of DateTime properties

I'm processing a number of items, each of which contain a DateProcessed property (a nullable DateTime) and want to Assert that the property is set to the current date. By the time it gets through the processing routine the dates are all slightly…
Andy Clarke
  • 3,234
  • 5
  • 39
  • 66
15
votes
4 answers

Can Fluent Assertions use a string-insensitive comparison for IEnumerable?

I've got a pair of Lists I'm trying to compare using Fluent Assertions. I can code up a comparison easily, but I'd like to use Fluent Assertions so that I can get the reason to show up in the test failed message. Everything I've seen so far seems to…
Zugbo
  • 514
  • 1
  • 4
  • 14
15
votes
2 answers

How to exclude multiple properties in FluentAssertions ShouldBeEquivalentTo()

Using FluentAssertions: I'm able to exclude a single property using ShouldBeEquivalentTo. x.ShouldBeEquivalentTo(y, opts => opts.Excluding(si => !si.PropertyInfo.CanWrite)); But, how do I exclude more then 1 property when using…
hannes neukermans
  • 12,017
  • 7
  • 37
  • 56
13
votes
2 answers

How to check a list is ordered using Fluent Assertions

I am writing some unit tests using specflow and need a way to check whether a list of objects is ordered by a specific property. Currently I am doing it like this, but I am not sure if this is the best way to go about it. var listFromApi =…
TomJerrum
  • 584
  • 1
  • 5
  • 20
13
votes
2 answers

FluentAssertions ShouldNotThrow is not recognised for an async method/Func

I am trying to check an async method throws concrete exception. For that I am using MSTEST and FluentAssertions 2.0.1. I have checked this Discussion on Codeplex and to see how it works with async-exception methods this another one link about…
ferpega
  • 3,182
  • 7
  • 45
  • 65
12
votes
4 answers

Extension methods overload choice

I have two extension methods: public static IPropertyAssertions ShouldHave(this T subject) { return new PropertyAssertions(subject); } public static IPropertyAssertions ShouldHave(this IEnumerable subject) { return new…
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
12
votes
4 answers

FluentAssertions: Assert Collection contains Element that "IsEquivalentTo"

I'm stuck with what I thought was an easy example. I want to assert that a collection of objects contains an object that is equivalent to a given object. like: col.ShouldContainEquivalentTo(obj) var objectList1 = new List { new…
Matthias
  • 1,267
  • 1
  • 15
  • 27
10
votes
2 answers

How to combine collection and property assertions using fluent-assertions?

I would like to "combine" Fluent Assertion's collection assertions and property assertions, e.g. assert that two IEnumerable's are pairwise-equal using property-by-property (possibly "nested") comparison (i.e. structural equality, in functional…
Stephen Swensen
  • 22,107
  • 9
  • 81
  • 136
10
votes
4 answers

Ambiguous Call when using Should().NotBeNull() on As item

When I do the following test var contentRes = res as OkNegotiatedContentResult>; contentRes.Should().NotBeNull(); I get the error The call is ambiguous between the following methods or properties:…
Charlie Hardy
  • 175
  • 1
  • 3
  • 14
10
votes
2 answers

FluentAssertions Should().BeEquivalentTo() fails in trivial case when types are C# 9 records, seemingly treating objects as strings

I started to use FluentAssertions recently, which supposed to have this powerful object graph comparison feature. I'm trying to do the simplest thing imaginable: compare the properties of an Address object with the properties of an AddressDto…
Leaky
  • 3,088
  • 2
  • 26
  • 35
10
votes
3 answers

FluentAssertions - how make ShouldBeEquivalentTo compare empty and null as equal

I am using Fluent Assertion library as part of my unit tests for some custom serialization code and I am looking for a way to force ShouldBeEquivalentTo to compare as equal a null and empty list. Basically, my tests look something like: [Test] …
Mike Sackton
  • 1,094
  • 7
  • 19
10
votes
2 answers

Use of `ShouldBeEquivalentTo`, `ShouldAllBeEquivalentTo`, and `BeEquivalentTo`

I am working with fluent assertions in my unit tests, however the use of ShouldBeEquivalentTo, ShouldAllBeEquivalentTo, and BeEquivalentTo is unclear. For example; all the following statements pass so the functions appear equivalent. List a…
James Wood
  • 17,286
  • 4
  • 46
  • 89
1
2
3
26 27