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

How to check equivalence using Fluent Assertion Should().BeEquivalentTo() when using derived classes

I'm having problems trying to get Should().BeEquivalentTo() to work with types that derive from a base class and implement a collection interface: public class Entity { public string Id {get; set;} public string Name {get; set;} } public…
Tim Calladene
  • 107
  • 12
1
vote
1 answer

How to show the variable name on the exception message when the assertion fails using FluentAssertions?

I am trying to create some Assertion classes using the FluentAssertions library. This is the Assertion code: public AndConstraint Work(string because = "", params object[] becauseArgs) { Execute.Assertion …
1
vote
1 answer

How does FluentAssertions compare 2 objects (Using Reflection or another way)?

I'm currently using FluentAssertion for comparing 2 objects. I really want to know what is the way it uses to compare? Using Reflection then loop all props like this? public static void PropertyValuesAreEquals(object actual, object expected) { …
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
1
vote
1 answer

Fluent Assertions message for mismatched item in a collection comparison

I am comparing two collections using fluent assertions and given that number of items match but the item property values don't match, i get messages saying Expected item[2].Required to be False, but found True. Now what i am looking to do is to…
Gurpreet
  • 1,382
  • 11
  • 24
1
vote
1 answer

FluentAssertions - Comparing objects containing properties with similar names but different types

I have several classes in our code that are automatically generated by the XSD generator tool from XSD file definitions. The classes look very similar with similar names, but based on the XSD schema (which we've received from an external vendor),…
1
vote
1 answer

FluentAssertions: How to break through drill-down of Which/And cascades?

Is there a way in FluentAssertions to avoid the automatic object graph drilldown for .And and .Which cascades? And some point of the drilldown I would like to go back to the root level and check the status code. Small code example: Func
timmkrause
  • 3,367
  • 4
  • 32
  • 59
1
vote
1 answer

How to get 100% test coverage over a DTO object class's getters when asserting function returns expected DTO object?

I'm unit testing an ASP.Net Core API by asserting that my repo layer returns a DTO object. As I'm testing behavior, I don't exactly care what values my DTO object has, so I'm autogenerating them using AutoFixture. I'm also trying to satisfy my…
1
vote
1 answer

Global equivalency config. Excluding members

I'm wondering if I can do a global config for a test. I know that I can compare this object something like that: x.Should().BeEquivalentTo(y, opt => opt.Excluding(z => z.Member) But I want all the methods in my test to use this config.
Sergеу Isupov
  • 504
  • 1
  • 8
  • 20
1
vote
1 answer

Exception when including properties with predicate

I'm trying to compare two objects with multiple properties, but need specific properties to be compared using a predicate (object1does not have exact values for those properties at object2, so I need to compare partial strings there). So, I'm…
YMM
  • 632
  • 1
  • 10
  • 21
1
vote
2 answers

Checking exceptions with TestCaseData parameters

I'm using NUnit 3 TestCaseData objects to feed test data to tests and Fluent Assertions library to check exceptions thrown. Typically my TestCaseData object contains two parameters param1 and param2 used to create an instance of some object within…
YMM
  • 632
  • 1
  • 10
  • 21
1
vote
3 answers

FluentAssertions: how to set breakpoint inside lambda

FluentAssertions is a great library but often I am very frustrated when some code in lambda is not working as expected and I cannot debug it. Especially when lambda is complicated. payload.Resource.Relations.Removed.Should().NotBeNull() …
Karel Kral
  • 5,297
  • 6
  • 40
  • 50
1
vote
1 answer

Print arbitrary property value in test output when collection comparison fails

When I compare collections of objects using .BeEquivalentTo() and there is a mismatch between properties in those collections, I get something like this in error output: Expected item[8].DaysExpiredMessage to be "In 8 days", but "In 9 days"…
YMM
  • 632
  • 1
  • 10
  • 21
1
vote
1 answer

Object structure comparison methods in Fluent Assertion

I noticed with the latest nuget package (FluentAssertions -Version 5.6.0) I still can't see the ShouldBeEquivalentTo method to do the object comparison without value (ONLY the structure). I can see Should().BeEquivalentTo() but not sure how to do…
Shabar
  • 2,617
  • 11
  • 57
  • 98
1
vote
3 answers

How to control "Equality" for dictionary members using FluentAssertions

Is there a way, with FluentAssertions, to control how the values of a dictionary are compared for equality ? I have a class, one property of which is a dictionary (string/double). I would like to compare two instances of the class (expected and…
Black Light
  • 2,358
  • 5
  • 27
  • 49
1
vote
2 answers

Fluent assertion should be greater than always pass

I am trying to test my collection using : var costByFactoryt = dataAccess.GetcostPerFactoryt(null, null); costByFactoryt.Count().Should().BeGreaterThan(0); costByFactoryt.Select(x => x.Cost.Should().BeGreaterThan(100)); But the problem is ,…
Simsons
  • 12,295
  • 42
  • 153
  • 269