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
3
votes
2 answers

Fluent Assertions - null and empty string comparison

Is it possible to force fluent assertions to pass Should().Be() for comparison between null and empty string? Or maybe this can be done somehow with BeEquivalentTo? Example: text1.Should().Be(text2); I want code above to pass when: text1 will be…
user14789187
  • 31
  • 1
  • 3
3
votes
2 answers

How to use the method When in FluentAssertions

Sometimes I have to assert that two lists have the same items. With fluent assertions this can be done like this: class MyObject { public string MyString {get; set;} } var o1 = new MyObject { MyString = "1 " } list1.Add(o1); var o2 = new…
3
votes
1 answer

How could I throw an exception using Fluent Assertions?

I am using a client to interact with CloudMQTT API. I am trying to create a user but after trying the code provided below, I was not able to create a user. When using the code provided within the Github repository for this project, I noticed that I…
Barrosy
  • 1,407
  • 2
  • 25
  • 56
3
votes
1 answer

How do I write CustomAssertion using FluentAssertions?

There is official example how to create CustomAssertion at FluentAssertions docs, however my attempt to apply it fails. Here's the code: public abstract class BaseTest { public List TestList = new List() { 1, 2, 3 }; } public class…
3
votes
0 answers

How to check that a collection contains N items that satisfy a given condition?

The Fluent Assertions framework provides a plethora of collection-related assertions. For checking whether a given collection contains an element that satisfies a specific condition, I can use Should().Contain: items.Should().Contain(item =>…
F-H
  • 663
  • 1
  • 10
  • 21
3
votes
1 answer

Fluent assertions for xml

I have a xml file of the form : I'm using XUnit to check the structure of the xml. [Fact] public void…
Dev
  • 1,451
  • 20
  • 30
3
votes
2 answers

Configure fluent assertions precision for comparing decimal

I am writing unit test using fluent-assertions and sqlite which stores badly decimal type. I would like ALL my decimal comparison to ignore the floating part. Is there a way to do it ? Thanks !
Jonathan ANTOINE
  • 9,021
  • 1
  • 23
  • 33
3
votes
1 answer

How to test MediatR handlers in XUnit with FluentAssertions

I am using XUnit for testing my ASP.NET Core 2.2 project. Along with it, I have FluentAssertions in the test project. What I want to do is to test my MediatR handler. Inside this handler I have API call. I have read through articles and it seems…
Franva
  • 6,565
  • 23
  • 79
  • 144
3
votes
1 answer

Check return value in FluentAssertion exception syntax

I'd like to check the return value from the method by FluentAssertion syntax. Please consider the following snippet: public interface IFoo { Task DoSomething(); } public class Bar { private readonly IFoo _foo; private static int…
rgb
  • 1,750
  • 1
  • 20
  • 35
3
votes
2 answers

C# Unit Testing - Assertions on JSON

I am just playing around with some Json and Fluentassertions, I am able to make a call to an API successfully, get the results, deserialize them but for some reason when i get to do an assertion on the response its losing the data and it empty. I…
Miko
  • 109
  • 1
  • 7
3
votes
1 answer

Customize failure handling in FluentAssertions

I'm trying to use FluentAssertions not only as testing assertions framework but also to check runtime contracts (like advanced Debug.Assert, and I have read this question). The desired behaviour is: In debug mode: throw exceptions (it does). In…
ArXen42
  • 139
  • 1
  • 8
3
votes
1 answer

How to use a specific comparer func for Equality in FluentAssertions

I'm writing an Akka.NET Testkit implementation that uses FluentAssertions under the hood, but can't figure out how to write the 'last' Assertion: Equality using a custom Func equality comparer (while getting a nice Error message, of course). public…
Bart de Boer
  • 381
  • 2
  • 7
3
votes
0 answers

FluentAssertions AssertionScope Context SubPath

Is there anyway to specify what gets appended to the {context} key of the built in assertions or potentially get the value of that context? Sample E.g. I managed to append to custom Assertions using AssertionSope keys to custom Execution.Assertions…
Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118
3
votes
2 answers

FluentAssertions factor out repeated config

I have unit-test code like this: // version3 should be a copy of version2 with some differences version3.Data.ShouldBeEquivalentTo(version2, option => option .Excluding(x => x.Id) .Excluding(x => x.Status) .Excluding(x => x.Stale) …
Jevon Kendon
  • 545
  • 4
  • 13
3
votes
2 answers

FluentAssertions Should().BeEquivalentTo doesn't compare run-time derived types on EF dynamic proxies

I'm using FluentAssertions to compare two objects using Should().BeEquivalentTo() where one object is an EF dynamic proxy. However, it appears that the unification of ShouldBeEquivalentTo and ShouldAllBeEquivalentTo (#593) in 5.0.0 has broken the…
Neo
  • 4,145
  • 6
  • 53
  • 76