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
0
votes
1 answer

Fluent Assertions how to Exclude parameters with generic object

I have this generic method for a test class that I want to exclude some common parameters between few entities. private static bool IsEquivalentTo(object request, object expectedRequest) { request.Should().BeEquivalentTo(expectedRequest, config…
0
votes
1 answer

FluentAssertions: collection subset should contain equivalent of list

I have a collection: new[] { new { A = 5, PropIDontCareAbout = "XXX" }, new { A = 7, PropIDontCareAbout = "XXX" }, new { A = 9, PropIDontCareAbout = "XXX" } } I want to check that it at least contains both new { A = 9 } and new { A = 5 } in any…
THX-1138
  • 21,316
  • 26
  • 96
  • 160
0
votes
1 answer

How to do an "AllNotSatisfy" condition in FluentAssertions

Consider this code: var ints = new List>() { new Dictionary() { { "1", "bill" }, { "2", "john" } }, new Dictionary() { { "2", "jane" }, { "3", "alex" } } }; This…
Michael Wiles
  • 20,902
  • 18
  • 71
  • 101
0
votes
2 answers

Assertion for check string value include array of string

I create the class of book as below public class book { public int id {get;set;} public string bookName {get;set;} } I define the list of book data : List books as [ {"id":1,"bookName":"falling apple"},{"id":2,"bookName":"fall…
Jack Man
  • 19
  • 3
0
votes
3 answers

Evaluate all properties in one assertion with the FluentAssertions framework

Context: We need to assert object responses with many properties which have many permutations and quite a few of those properties are dynamic (generated GUIDs etc). Example Scenario When using FluentAssertions ...Should().BeEquivalentTo(...) one is…
Jay
  • 3,373
  • 6
  • 38
  • 55
0
votes
2 answers

Fluent assertions in c# How to assert anonymous Type

We are trying the Test Driven Development using Fluent Assertions for our .Net core 3.1 Web API and using XUnit for the same. This is what my controller returns. { "usersResult": [ { "UserId": "1", "UserName": "Foo" …
Ann Snow
  • 63
  • 1
  • 7
0
votes
2 answers

Assert anonymous object equivalence

I'm sure i'm missing the obvious... Say we have: [Fact] public void SomeTest() { var a = new { SomeProp = "hello", AnotherProp = 9 }; var b = new { SomeProp = "hello" }; var c = new { AnotherProp = 9 }; var d = new { SomeProp =…
Lee Tickett
  • 5,847
  • 8
  • 31
  • 55
0
votes
2 answers

Assert object equivalence by value

I'm sure i'm missing the obvious... Say we have: public class MyObject { public string SomeProp { get; set; } public int AnotherProp { get; set; } } [Fact] public void SomeTest() { var a = new MyObject { SomeProp = "hello", AnotherProp…
Lee Tickett
  • 5,847
  • 8
  • 31
  • 55
0
votes
1 answer

Mocking an interface derived from IList to pass it to Should().BeEquivalentTo()

I am currently testing a method that returns an instance of the IBuilding interface defined like this: public interface IBuilding { public string Name { get; set; } public IBuildingZones Zones { get; set; } } public interface…
OBones
  • 310
  • 2
  • 13
0
votes
2 answers

Cannot read dynamic properties with FluentAssertions

I am using XUnit and fluentassertions in c sharp for my unit tests. Below is where I get a dynamic type, convert a dynamic object to that dynamic type and try to do an assertion: var dynamicType = Type.GetType(...); dynamic?…
avdeveloper
  • 449
  • 6
  • 30
0
votes
2 answers

Assert when property has NullValueHandling.Ignore

On my endpoint's response I need to omit a property if its value is null , so I have tagged the prop with the [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] tag. So, with the tag on the property, this property is not going to be part…
0
votes
2 answers

FluentAssertions 6 ObjectGraph compare Enum to String

With FluentAssertions 6 it seems you can longer verify if in a object graph if an Enum is equivalent to a string. Source: https://fluentassertions.com/upgradingtov6 enum MyEnum { A, B } class Source { MyEnum Enum { get;set;} } class…
0
votes
1 answer

How to use Exclude in FluentAssertions for property in collection of collections?

I have two records: public record A(string AId, IReadOnlyList list) public record B(string BId, string Color) I want to use fluent assertions to compare collections of instances of A, while excluding the proberty BId e.g: Let's say I…
omar kh
  • 11
  • 4
0
votes
1 answer

Asserting a collection of objects is a part of another collection of objects of the same type

I'm trying to use .BeSubsetOf() from Fluent Assertions to ensure that one collection of objects is a subset of another, bigger collection of objects of the same type, but the assertion always fails. Example code: var expected = new[] { …
YMM
  • 632
  • 1
  • 10
  • 21
0
votes
1 answer

Fluent assertion to verify DateTime field is not empty

var dte = "2021-12-18T15:06:33.2677927Z" dte.Should(). I want to check that this dte is not empty. Currently I am using : dte.Should().BeAfter(new DateTime());
purplized
  • 61
  • 1
  • 7