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.

Filter by
Sorted by
Tagged with
2 votes
1 answer
47 views

Fluent Assertions - Assert properties with same names but different types

I get this (expected) error when asserting properties with different types: Expected property a.Date to be System.String, but found System.DateTime. I would like to assert the equality between a ...
Vidar Flesjø's user avatar
0 votes
0 answers
21 views

Using FluentAssertions to find null in object graph

I am trying to use the extensibility APIs (IEquivalencyStep) in FluentAssertions to detect if an object recursively contains any property with null value. The object type is unknown at compile time. I ...
Oskar's user avatar
  • 7,975
0 votes
0 answers
15 views

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 ...
rklec's user avatar
  • 83
1 vote
1 answer
48 views

Testing ValueTask method with FluentAssertions, should not throw

I'm a bit confused here. I'm using FluentAssertions and I have this test method: [Test] public void DoNothingWhenEmpty() { FileCleanUpProcessor fileProcessor = new(); Func&...
AlexB's user avatar
  • 4,207
0 votes
0 answers
19 views

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<string>(); var ...
user1263981's user avatar
  • 2,963
0 votes
1 answer
18 views

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; ...
SuperJMN's user avatar
  • 13.2k
0 votes
0 answers
15 views

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)...
DerHandwerk's user avatar
0 votes
1 answer
49 views

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 ...
Erik Philips's user avatar
  • 53.5k
1 vote
2 answers
46 views

How to use FluentAssertions to test for the fact that all properties should have been modified without having any known/fixed target property values

I want to use FluentAssertion's object graph comparison to assert the fact that all properties have been changed/modified to any value I don't care about the target value. Actually, to highlight that,...
rklec's user avatar
  • 83
0 votes
0 answers
32 views

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. ...
Petr Nečas's user avatar
0 votes
1 answer
30 views

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. ...
g.pickardou's user avatar
  • 32.5k
0 votes
2 answers
24 views

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 ...
Lodewijk's user avatar
  • 2,382
0 votes
1 answer
27 views

Exclude EnumerableEquivalencyStep on top level Assertion

I have this object from an external library that implements IEnumerable<T>. I just want to check the properties of this object and not the equivalence of the collection. I'm not interested in ...
Lodewijk's user avatar
  • 2,382
0 votes
1 answer
60 views

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, ...
Andrew's user avatar
  • 1,139
1 vote
1 answer
37 views

How to check nullable int against nullable enum in unit test assertion?

Writing some integration tests we want to compare values read from the DB (int) against the object being used (enum field on a DTO). So conceptually dto.EnumValue.Should().Be((MyEnum)expectedIntValue) ...
Mr. Boy's user avatar
  • 60.9k
1 vote
0 answers
48 views

Getting "&#39" instead of string in output of VSTest@2 in test run where soft assertion fails

I use Fluent Assertions in Visual Studio when I write automated tests in C#/Selenium. I HAVE PLACED AN INTENTIONAL FAILED ASSERTION IN MY CODE to demonstrate the problem. The output looks fine on ...
Chris LaVenture's user avatar
2 votes
4 answers
218 views

How to mock exception types in "Throws" and "ThrowsAsync"

I would like to write one unit test for a set of different exception types. My common approach is to use InlineAutoMoqData for a certain set of test inputs. This works fine for values but I cannot get ...
xMutzelx's user avatar
  • 566
2 votes
1 answer
72 views

FluentAssertions: Custom Double Comparer

I try to get started with FluentAssertions but quite soon I ran in a problem. I use it along with MSTest. The test below fails what shouldn´t happen (from my point of view). [TestMethod] ...
ProtonFisher's user avatar
0 votes
0 answers
45 views

How to increase the context of the line difference in FluentAssertions

For example, I have the first "Good day brother" and the second "Good day sister" line, where in FluentAssertions, when calling the Should().Be() method, I will get the following ...
GuardianDead's user avatar
0 votes
1 answer
112 views

C# Deeply Compare Nested Collections with FluentAssertions

How can I write a unit test such that the following two objects are considered equal? And in general how can I ensure collections nested arbitrarily deeply are compared against counterpart via their ...
AffluentOwl's user avatar
  • 3,337
0 votes
0 answers
226 views

How to assert some combinations of Enum Values are true while all other should be false in xUnit

In short: Is there an easy way to make a unit test to assert a combination of 2 Enum value combinations results in true while all other combinations results in false? Detailed situation: There is a ...
this.myself's user avatar
  • 2,111
0 votes
1 answer
85 views

How can I assert the duration of the synchronous part of an async call?

I need to assert that the initial synchronous part of an async method executes within a certain duration. Is this possible with fluent assertions? I also need to assert the minimum and maximum total ...
tg73's user avatar
  • 415
0 votes
1 answer
155 views

How do I assert that an async task should complete within a minimum and maximum time?

I can assert that a task should complete within a specified maximum time like this: await obj.Awaiting( o => o.DoSomethingAsync() ).Should().CompleteWithinAsync( 1.Second() ); But how can I also ...
tg73's user avatar
  • 415
0 votes
0 answers
23 views

FluentAssertions GraphComparison, all properties of only output object should be filled

Often in my team people forget to map certain fields from an input object to an output object. I wanted to write a library for unit testing, that checks if all properties on an output object have been ...
BartKrul's user avatar
  • 577
1 vote
1 answer
136 views

Mock Azure File Share CreateAsync method in unit test

I have the following method, that I'm trying to write some unit tests public async Task<ShareClient> CreateFileShare(string shareName) { if (!ShareNameValidator.IsValid(shareName)) ...
Pamador's user avatar
  • 158
0 votes
1 answer
70 views

FluentAssertions: Assert all object properties are different from another object

Is there any way to assert that all properties of objectA are different from all properties of objectB using FluentAssertions? objectA and objectB are instances of the same class. .BeEquivalentTo() ...
Andrei's user avatar
  • 450
0 votes
0 answers
14 views

Display discrepancies between collections

When asserting the equality of two string collections using collectionA.Should().Equal(collectionB) the message is: "Expected collectionA to be equal to {"a", "b", "c&...
Nafas's user avatar
  • 183
0 votes
2 answers
148 views

FluentAssertions: Test if an Enumerable contains "subset" of itself

I'm looking for the elegant expression to test if an enumerable contains a 'subset' of itself. Lets me illustrate with a sample: [Fact] public void Test1() { // Arrange var expected = new[] ...
dani herrera's user avatar
  • 48.9k
0 votes
0 answers
25 views

How can I used BeEquivalentTo to make null equivalent to empty?

How can I used BeEquivalentTo to compare ActualObject.Flied=null and ExpectedObject.Filed = String.Empty and get they are equivalent? I am looking for something like ActualObject.Should()....
Lola Flores's user avatar
-1 votes
1 answer
80 views

NUnit tests failed with FLuentValidation in ONion Architecture

Im trying to implement nunit tests with fluentvalidations, but it shows me a failed test and the message: FluentValidation.AsyncValidatorInvokedSynchronouslyException : Validator "...
Stefan0309's user avatar
  • 1,612
0 votes
1 answer
145 views

Fluent assertions event monitoring does not work

I try to using following features for my unit testing https://fluentassertions.com/eventmonitoring/ But I catch a strange exception on execution line. Message:  System.InvalidProgramException : ...
Артур Малюга's user avatar
1 vote
0 answers
124 views

FluentAssertions cast type to another on the fly when comparing

I'm facing some issues when using FluentAssertions lib to make a equivalency check on two System.Data objects. I have this scenario: Columns.OfType<DataColumn>().Should().BeEquivalentTo(Columns....
therealsowho's user avatar
-2 votes
2 answers
441 views

C# unit testing MassTransit handler with MSTest, Moq and FluentAssertions. Can't verify method called exactly once

I have this class called Handler, which is a MassTransit IConsumer: public class Handler : IConsumer<ICommand> { private readonly IOrderRepository _orderRepository; public Handler(...
benjamin's user avatar
  • 1,374
0 votes
1 answer
179 views

FluentAssertions .Pass declaration

After performing a test, we typically do an Assert.True(someCondition). When a condition check isn't necessary and just completion of the test is sufficient, I end a test with Assert.Pass(). I'm ...
BillRob's user avatar
  • 4,669
2 votes
1 answer
220 views

FluentAssertion, check constraint just for condition

Inside a c# test I'm asserting with FluentAssertion. But, in this test, the "assert" only should be executed under a condition (when result is not valid) This is my actual code: if (!result....
dani herrera's user avatar
  • 48.9k
0 votes
0 answers
216 views

Can I assert that certain Http requests were made BEFORE or AFTER other Http requests in an Xunit unit test?

I have a simple Xunit project setup to test my code. During the method under test, I make two, separate, consecutive Http requests to an endpoint on the network. My test should check that the first ...
Ben Smith's user avatar
0 votes
1 answer
31 views

IEnumerable.GetEnumerator unit Test

My part of code: public class BinaryTree<T> : IEnumerable<T>, IReadOnlyCollection<T> where T : IComparable { ......... public IEnumerator<T> ...
Izauma's user avatar
  • 3
0 votes
1 answer
116 views

How to capture an exception thrown by an invocation using FluentAssertions

In FluentAssertions, how does one capture an exception thrown by an invocation? Exceptions shows basic code samples, but in my case I have a CustomException with a public enum. I'd like to assert that ...
user246392's user avatar
  • 2,401
1 vote
1 answer
396 views

How to exclude multiple properties from comparison and diff check objects efficiently in FluentAssertions

In FluentAssertions, I can compare the object graphs of two objects of the same type and exclude properties from the comparison. order.Should().BeEquivalentTo(anotherOrder, options => options....
user246392's user avatar
  • 2,401
1 vote
0 answers
29 views

Collection assertion, can error message include "name" (or .ToString) of missing/extra item from list?

I have two lists (of objects, but later selected just into "Name" strings) and I'm using Fluent Assertions to compare that they should contain the same items. This is easily enough achieved ...
gakera's user avatar
  • 3,589
0 votes
1 answer
79 views

FluentAssertions - "nested" Exclude

I have an API providing tons of functionality, thatswhy there are massive abstractions to generics. Please imagine (with the code below) following scenario: Every Dto has an Id. I want to test against ...
Robert Schmidt's user avatar
0 votes
2 answers
309 views

How to exclude a JSON property from comparison

How to exclude a JSON property from comparison in FluentAssertions.Json? JToken token, expectedJson; token.Should().BeEquivalentTo(expectedJson); { "property1":"value1", "...
Andrej B.'s user avatar
  • 187
0 votes
0 answers
82 views

How do I mock Func<ClientWebSocket> with NSubstitute?

I have the following xUnit test. How should I do in order to mock Func<ClientWebSocket> with new ClientWebSocket() using NSubstitute? Btw, in addition ClientFactory doesn't want to 100% unit ...
nop's user avatar
  • 4,751
0 votes
1 answer
205 views

IEnumerable swallows an exception and a unit test cannot catch it

I'm unit testing the following method and I expect ArgumentOutOfRangeException to be thrown. Instead gridTrading.GridLevels().As<double[]>() returns null for whatever reason. It basically ...
nop's user avatar
  • 4,751
1 vote
2 answers
397 views

FluentAssertions - Check if all properties have default values when given empty input

As you can see in the unit test below, I'm basically checking if all properties have default values. Is there a more fluent way in FluentAssertions to do so that I'm not aware of? This test aims to ...
Hulkstance's user avatar
  • 1,323
1 vote
1 answer
150 views

Collect assertion failures using Fluent assertions in C#

My goal is to use fluent assertions to collect all assertion failures and report them. Currently, I use private static void AssertValue(object? actual, object? expected, string fieldName, IDictionary&...
Patrik Mihalčin's user avatar
0 votes
0 answers
39 views

A list of products whose description contains any of the keywords in a string array to FluentAssertions

I wanted to wrap the LINQ query in the AllSatisfy condition. How do I make it filter the products containing any of the keywords? I did it and it works, but I wonder how to wrap it in the AllSatisfy ...
nop's user avatar
  • 4,751
0 votes
1 answer
99 views

FluentAssertions alternative of TrueForAll

I'm trying to find an alternative of .TrueForAll using FluentAssertions. Note that in the Shoudly example Products is List<Product> whereas in the new code it is IEnumerable<Product>. It ...
nop's user avatar
  • 4,751
0 votes
1 answer
491 views

Regex.IsMatch to MatchRegex in FluentAssertions

I looked at the documentation and there were methods e.g.Match, MatchRegex, etc., so I decided to improve the commented statement below and replace Regex.IsMatch with MatchRegex. However, I'm getting ...
nop's user avatar
  • 4,751
1 vote
0 answers
59 views

In FluentAssertions, should the result or expectation come first in x.Should().BeEquivalentTo(y)

Given a class, and a class I want to map to: public class A { public string Prop1 { get; set; } public string Prop2 { get; set; } } public class B { public string Prop1 { get; set; } ...
FBryant87's user avatar
  • 4,283

1
2 3 4 5
8