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
1
vote
1 answer

Assert that list should not have the string

I have a assertion like this: validationResults.Select(result => result.Tag).ToList().Should().Contain(ServiceContractRuleKey.MedicalDeclarationNumberRequired "because a validation error should be added that the MedicalDeclarationNumber is…
Djave
  • 277
  • 1
  • 5
  • 14
1
vote
2 answers

Searching for nicer implementation for this unit test

I use xUnit and FluentAssertions to write my unit tests and I am stuck at the following problem. As I have not implemented the catch (in GetCountriesAsync) of the WebException yet, I throw a new NotImplementedException in this place. This code is…
Michael Schnerring
  • 3,584
  • 4
  • 23
  • 53
1
vote
1 answer

Exclude uninitialized (null) properties in Fluent assertion

In FluentAssertions I can use the AllProperties.But(obj => obj.property_I_do_not_want) to remove specific properties from a comparison assertion which is fine when I know the names of the properties I want to ignore but in my situation I only want…
carlin.scott
  • 6,214
  • 3
  • 30
  • 35
1
vote
1 answer

How do I test a controller to ensure that the authorize filter is applied to it with the correct role?

I have a controller in the admin section of my site and it is decorated with the Authorize filter with the role set to admin. [Authorize(Roles = "Admin")] public class SubscriberController : Controller This works great but I'd like to create a unit…
Jack Hughes
  • 5,514
  • 4
  • 27
  • 32
0
votes
1 answer

FluentAssertions comparing contents of lists instead of lists themselves

I have two objects (instances of the same class) with a bunch of properties, some of them lists of other objects. class A { public int a { get; set; } public string b { get; set; } public IList cs { get; set; } } I want to compare…
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
0
votes
0 answers

Throw on additional members in source object with FluentAssertions which are not compared, i.e. assert all properties are in both objects?

Background of the library: Basically I want to compare an object with FluentAssertions BeEquivalentTo. Now, FluentAssertions Object graph comparison works in a way that is usually good for unit testing: If your source object has additional…
rklec
  • 73
  • 10
0
votes
0 answers

Equivalent of mock verifySet when using FluentAssertion

I'm currently using FluentAssertion and wondering what should be the equivalent of mock verifySet? [TestMethod] public void Add() { // Arrange var key = _fixture.Create(); var value = new object(); var session = new…
user1263981
  • 2,953
  • 8
  • 57
  • 98
0
votes
1 answer

How to check for same type in nested properties with FluentAssertions?

I'd like to tell FluentAssertions to check if 2 instances are of the same type, but for a nested property. The scenario is the following: This is the class class Item { public int? Value { get; set; } public Exception? Exception { get; set;…
SuperJMN
  • 13,110
  • 16
  • 86
  • 185
0
votes
0 answers

Assert Success Message

is there a possibility to not "only" show messages in case a assertion fails but also in case an assertion was successful? 42.Should().Be(42); // could show something like this: 42 is 42 (OK) I have already looked through the code but I have only…
DerHandwerk
  • 99
  • 3
  • 10
0
votes
1 answer

Correctly asserting validation throws a validation exception

I have this code "dummied" down to it most basic parts but I'm not sure how to resolve the the unit test fails. Mostly because the error message suggestion doesn't match what I'm actually doing. DotNetFiddle Example using System; using…
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
0
votes
0 answers

FluentAssertions.Analyzers Not Providing Expected Suggestions or Messages

I have a simple C# .NET 6 Xunit test project and am using FluentAssertions to verify the expected outcomes of my tests. I've been trying to use FluentAssertions.Analyzers to analyze these assertions. However, the following test doesn't trigger any…
Petr Nečas
  • 426
  • 3
  • 11
0
votes
1 answer

How to use Fluent Assertions Should().Throw() with NOT hardcoded exception type?

I would like to use fluent assertions .Should().Throw, but do not want to hardcode the type of the expected exception, instead I want it parametrized, so it is in a variable which type is Type. Without parametrized it should be the following: Action…
g.pickardou
  • 32,346
  • 36
  • 123
  • 268
0
votes
2 answers

FluentAssertions Defaults for type

I'm testing complicated object graphs with FluentAssertions and I want to set defaults for ShouldBeEquivalentTo. The problem is that the object graph re-used the same base-type for a lot of the properties. I just want to set up my AssertionOptionss…
Lodewijk
  • 2,380
  • 4
  • 25
  • 40
0
votes
1 answer

Exclude EnumerableEquivalencyStep on top level Assertion

I have this object from an external library that implements IEnumerable. I just want to check the properties of this object and not the equivalence of the collection. I'm not interested in the stuff inside the IEnumerable. How can I exclude this…
Lodewijk
  • 2,380
  • 4
  • 25
  • 40
0
votes
1 answer

FluentAssertions ExcludingNestedObjects option does not behave as expected

Consider the following C# code: class Parent { public int Id { get; set; } public Child Child { get; set; } } class Child { public int Id { get; set; } } var p1 = new Parent { Id = 0, Child = new Child { Id = 1 } }; var p2 = new Parent…
Andrew
  • 1,139
  • 1
  • 12
  • 27