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

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 CreateFileShare(string shareName) { if (!ShareNameValidator.IsValid(shareName)) { throw new…
Pamador
  • 158
  • 2
  • 8
1
vote
0 answers

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().Should().BeEquivalentTo(Columns.OfType(), options => options …
therealsowho
  • 133
  • 1
  • 7
1
vote
1 answer

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.Excluding(o => o.Customer.Name)) …
user246392
  • 2,661
  • 11
  • 54
  • 96
1
vote
0 answers

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 through the Collection Assertions, such as .Equal or…
gakera
  • 3,589
  • 4
  • 30
  • 36
1
vote
2 answers

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 check whether it throws or not when given no…
Hulkstance
  • 1,323
  • 9
  • 15
1
vote
0 answers

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; } public string Prop2 { get; set; } public…
FBryant87
  • 4,273
  • 2
  • 44
  • 72
1
vote
1 answer

How to modify value being asserted inside of the config

I am wondering if there is a way to modify asserting value in the config. For example, I have the following assertion customer.Should().Be(c, config => config.Excluding(c => c.Updated)); customer.Updated.Should().Be(c.Updated.ToString()); Is there…
Alex
  • 53
  • 4
1
vote
2 answers

Xunit.net: Validating model returned by an async ASP.NET method

I have been writing unit testing for the API. I need to check whether values inside the model returned by the action method are expected or not. [Fact] public async Task MerchantController_GetMGetProductByMerchantId_Merchant() { //Arrange …
1
vote
0 answers

FluentAssertions BeEquivalentTo: Exclude missing members on expectation

I'd like to assert that my dto (which is flat object with few properties) is equivalent to my entity (which is complex object with nested properties). public class Dto { public string A {get;set;} public string B {get;set;} } public…
Shumek
  • 56
  • 4
1
vote
2 answers

FluentAssertions error CS0012: You must add a reference to assembly System.Data

I added the nuget FluentAssertions 6.7.0 in a test project using .NET Framework 4.6.1. I run tests from Rider 2022.1.1. I'm new to this nuget and I read the intro and searched for issues (none found). I come from the Should family and trying to…
SandRock
  • 5,276
  • 3
  • 30
  • 49
1
vote
1 answer

Weird FluentAssertions behaviour with record classes

I have the following test that surprisingly passes:   abstract record class Base(string Property1); sealed record class SubClassA(string Property1) : Base(Property1); sealed record class SubClassB(string Property1, string Property2) :…
Ynv
  • 1,824
  • 3
  • 20
  • 29
1
vote
1 answer

How can I dynamically select properties for equivalency test - FluentAssertions

I'm creating unit tests in which I will be comparing lists of objects with one another. Currently I am using Fluent assertions in combination with specflow and nunit. I already use the Fluent Assertions to make a comparison as following: public void…
martijn
  • 1,417
  • 1
  • 16
  • 26
1
vote
2 answers

Unexpected behaviour using BeEquivalentTo in Fluent Assertions

I'm having an issue with fluent assertions that seems to go against what the documentation is stating. I have this simple case to show the problem. public class UnitTest1 { [Fact] public void Test1() { var test = new Test { Name…
scottdavidwalker
  • 1,118
  • 1
  • 12
  • 30
1
vote
1 answer

Unit test of the second method call

I have a unit test where I am using Moq and Fluent Assertions: [Fact] public void GetSymbols_ShouldSetSucceedToTrue_WhenSecondAttemptSucceed() { string selectedFileName = "testFileName.txt"; string[] expectedResult = new string[] {…
Qwertyluk
  • 363
  • 3
  • 14
1
vote
1 answer

FluentAssertions for equality of objects graph with array - approximate equality

I am trying to write a set of tests for an API which returns a result in the following form { "metrics": [ { "key": "Metric1", "portfolios": [ { "portfolioKey": "Portfolio1", "outputs": [ …
Jamiec
  • 133,658
  • 13
  • 134
  • 193