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

How to keep method return type 'void' in F#?

I'm writing unit tests for my F# library using F#, Visual Studio Unit Testing Framework (aka MSTest) and FluentAssertions. Test method should have return type either void or Task. In C# that's easy: [TestMethod] public void TestMethod1() { …
abatishchev
  • 98,240
  • 88
  • 296
  • 433
9
votes
2 answers

Satisfy() in Fluent Assertions does not work with collections of class objects

I have a class: public class TestClass { public int Id { get; set; } public int CampusId { get; set; } public int CurrentStudentCount { get; set; } public int MaxStudentCount { get; set; } } and a collection of…
YMM
  • 632
  • 1
  • 10
  • 21
9
votes
5 answers

Using FluentAssertions NotBeNull isn't taken into account by nullable analysers

When I use Fluent assertions to test if a property is not null, the analysers still complain about subsequent lines which dereference that line as possibly null. Is there any way to get the compiler to recognise the property as not being null after…
Mog0
  • 1,689
  • 1
  • 16
  • 40
9
votes
2 answers

Can I tell FluentAssertions to ignore Equals method when using BeEquivalentTo

I have a simple class with two properties and the Equals method overridden: public class Person : IEquatable { public Guid Id { get; set; } public string Name { get; set; } public override bool Equals(object obj) { …
Johan Vergeer
  • 5,208
  • 10
  • 48
  • 105
9
votes
2 answers

Fluent Assertions: Compare two numeric collections approximately

I have two arrays of double. Is there a way using FluentAssertions to compare the arrays element-by-element, using the .BeApproximately() technique? One range value would suffice for the entire array. Example: double[] source = { 10.01, 8.01, 6.01…
David H
  • 1,461
  • 2
  • 17
  • 37
9
votes
2 answers

Is testing generic collections for referential equality in C# a silly idea?

I'm implementing a special case of an immutable dictionary, which for convenience implements IEnumerable>. Operations that would ordinarily modify the dictionary should instead return a new instance. So far so good. But when I…
David Moles
  • 48,006
  • 27
  • 136
  • 235
8
votes
1 answer

FluentAssertions Should().BeOfType() or derived type?

I have the following situation: class A { } class B : A { } I want to assert that variables of typeof(B) are assignable to typeof(A) variables. How to do that with fluent assertions?
D.R.
  • 20,268
  • 21
  • 102
  • 205
8
votes
2 answers

Can FluentAssertions ShouldBeEquivalent work with c#9 record types?

I've been messing around with some of c#9's new features, and I've run into something that's less than fun. If I try to use Should().BeEquivalentTo() on a record with a DateTimeOffset, setting the Using option for DateTimeOffset to use BeCloseTo,…
8
votes
1 answer

FluentAssertions Throw() not listed to use

I'm using FluentAssertions with NUnit and I realize that the method Throw() and other related methods is not listed for me to use. Do I have to install any other package to have access to this method? I'm using the last release, 5.4.2, installed by…
8
votes
1 answer

FluentAssertions Should().BeEquivalentTo() fails with Lists containing run-time specified types deriving from identical interface

I have the following test illustrating a simple example of what im attempting to achieve (comparison of two equivalent lists): [Fact] public void Test() { // Arrange var list1 = new List { new BusinessCreatedDomainEvent { Name…
Mike Hawkins
  • 2,144
  • 5
  • 24
  • 42
8
votes
4 answers

How to assert that all selected properties are set (not null or empty)

I want to verify (assert) that certain properties on my DTO object are set. I was trying to do it with Fluent Assertions, but the following code does not seem to work: mapped.ShouldHave().Properties( x => x.Description, ...more x =>…
TarasB
  • 2,407
  • 1
  • 24
  • 32
8
votes
1 answer

How do I exclude a property of all items in IEnumerable when using ShouldBeEquivalentTo?

In my NUnit/FluentAssertions tests I compare the complex object returned from my system with a reference one using the following code: response.ShouldBeEquivalentTo(reference, o => o.Excluding(x => x.OrderStatus) …
kojo
  • 757
  • 7
  • 14
7
votes
2 answers

How to verify that multiple sorts have been applied to a collection?

I'm implementing sortable columns on my Kendo grid and the user-expected behaviour is to allow multiple columns to be sorted at the same time. Naturally, I'm starting off by writing a unit test to be able to verify that the sorting is (by default)…
Ilessa
  • 602
  • 8
  • 27
7
votes
2 answers

How to chain multiple 'And' when checking exception with FluentAssertions

I have a unit test that validates that some code throws an exception and that two properties have the expected value. Here is how I do it: var exception = target.Invoking(t => t.CallSomethingThatThrows()) …
mabead
  • 2,171
  • 2
  • 27
  • 42
7
votes
1 answer

FluentAssertions: int.Should().Equals returns wrong result?

I just start to use Moq & FluentAssertions and find this: results.Results.Count.Should().Equals(1); in the code, results.Results return a list of class List. In the test setup, I set it as results.Results.Count = 3 (I can see this # is correct in…
urlreader
  • 6,319
  • 7
  • 57
  • 91
1 2
3
26 27