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
95
votes
5 answers

Testing for exceptions in async methods

I'm a bit stuck with this code (this is a sample): public async Task Fail() { await Task.Run(() => { throw new Exception(); }); } [Test] public async Task TestFail() { Action a = async () => { await Fail(); }; …
Eugene
  • 1,073
  • 1
  • 7
  • 7
80
votes
11 answers

How to use Exclude in FluentAssertions for property in collection?

I have two classes: public class ClassA { public int? ID {get; set;} public IEnumerable Children {get; set;} } public class ClassB { public int? ID {get; set;} public string Name {get; set;} } I want to use fluent assertions to…
Liath
  • 9,913
  • 9
  • 51
  • 81
57
votes
8 answers

FluentAssertions: equivalence of sorted lists

I'm trying to establish equivalence of two lists using FluentAssertions in C#, where two things are of importance: the elements are compared by the values they hold, not by reference (i.e. they are equivalent, not equal) the order of the elements…
Samuel Neugber
  • 1,041
  • 1
  • 11
  • 17
54
votes
2 answers

How to use Fluent Assertions to test for exception in inequality tests?

I'm trying to write a unit test for a greater than overridden operator using Fluent Assertions in C#. The greater than operator in this class is supposed to throw an exception if either of the objects are null. Usually when using Fluent Assertions,…
Nathan Bierema
  • 1,813
  • 2
  • 14
  • 24
47
votes
1 answer

FluentAssertions: ShouldBeEquivalentTo vs Should().Be() vs Should().BeEquivalentTo()?

Can anybody summarize differences and usage scope between them? I read SO articles, ShouldBeEquivalientTo(): ShouldBeEquivalentTo() is intended to be used for comparing complex object graphs rather than the primitive types part of the .NET…
Youngjae
  • 24,352
  • 18
  • 113
  • 198
39
votes
4 answers

FluentAssertions Asserting multiple properties of a single object

Is there a way to do something like this using FluentAssertions response.Satisfy(r => r.Property1== "something" && r.Property2== "anotherthing")); I am trying to avoid writing multiple Assert statements. This was possible with…
kolhapuri
  • 1,581
  • 4
  • 20
  • 31
33
votes
3 answers

How to assert all items in a collection using fluent-assertions?

Say I want to test a method returning a bunch of items of the following type using fluent-assertions to ensure that all items have their IsActive-flag set to true: public class Item { public bool IsActive { get; set; } } To achieve that I could…
Spontifixus
  • 6,570
  • 9
  • 45
  • 63
29
votes
7 answers

How to compare two Json objects using C#

I have two Json objects as below need to be compared. I am using Newtonsoft libraries for Json parsing. string InstanceExpected = jsonExpected; string InstanceActual = jsonActual; var InstanceObjExpected = JObject.Parse(InstanceExpected); var…
Nandakumar1712
  • 357
  • 1
  • 4
  • 11
28
votes
3 answers

Is there a more appropriate to test if the constructor throws an exception?

Normally you test, if an exception gets thrown in a certain method, as follows. I use FluentAssertions: [Fact] public void Exception_gets_thrown() { // Arrange var foo = new Foo("validArgument"); // Act/Assert foo.Invoking(f =>…
Michael Schnerring
  • 3,584
  • 4
  • 23
  • 53
27
votes
4 answers

Fluent assertion for OR condition

I am trying to set up the fluent assertion for the below condition. But couldnt find a method with expression or an ObjectAssertion with Or(). I got to check the status of my service is of enum value Pending or…
Raghav
  • 2,890
  • 7
  • 36
  • 56
26
votes
4 answers

Multiple assertions using Fluent Assertions library

It seems that Fluent Assertions doesn't work within NUnit's Assert.Multiple block: Assert.Multiple(() => { 1.Should().Be(2); 3.Should().Be(4); }); When this code is run, the test fails immediately after the first assertion,…
YMM
  • 632
  • 1
  • 10
  • 21
23
votes
2 answers

How to compare lists using fluent-assertions?

I want to compare a list of objects, ignoring the order of the objects in the list and only comparing some of the properties in the objects, currently I'm using the following code to perform this comparison:…
setebos
  • 521
  • 1
  • 4
  • 10
21
votes
1 answer

FluentAssertions Type check

I try to use FluentAssertions to check in my UnitTest, that the type of a property in a list of items is of a certain type. myObj.Items.OfType().Single() .MyProperty1.GetType() …
xeraphim
  • 4,375
  • 9
  • 54
  • 102
20
votes
3 answers

How to replace Assert.Fail() with FluentAssertions

We are currently converting some code that was using Assert.IsTrue(), Assert.AreEqual(), Assert.IsNotNull(), etc. The basic unit test assert Library for C# We want to use FluentAssertions, like value.Should().BeNull(). I'm stuck on a few tests using…
Kaito Kid
  • 983
  • 4
  • 15
  • 34
19
votes
2 answers

NUnit or Fluent Assertions test for reference equality?

I'm using NUnit 2.6.2 + Fluent Assertions 2.0.1. I want to assert that two references do NOT point to the same object instance. I can't find a clean way to express that. NUnit has Assert.ReferenceEquals(ref1, ref2) - but I can't find the negated…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
1
2 3
26 27