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

Comparing Enums with custom Fluent Assertions Equivalency Step

I'm trying to write a custom Equivalency Step for Fluent Assertions to compare Enum values on the subject side back to a string on the exception side. The problem I seem to be facing is that the subject type passed in to the IEquivalencyStep has…
Kevin Smith
  • 13,746
  • 4
  • 52
  • 77
5
votes
1 answer

Opposite of ShouldBeEquivalentTo

I am writing unit tests in c#. ShouldBeEquivalentTo makes sense for positive cases, but for negative cases what do you use> I have not found anything like ShouldNotBeEquivalentTo.
Aaron
  • 4,380
  • 19
  • 85
  • 141
5
votes
3 answers

FluentAssertions, making sure IEnumerable contains only single element

I am writing unit tests and I have something that looks like this: [Fact] public void GetFoos_only_gets_foo1() { _foo1.included = true; //Foo object _foo2.included = false; //Foo object _sut.GetFoos().Should().OnlyContain(_foo1); // this…
user2958542
  • 331
  • 1
  • 8
  • 18
5
votes
2 answers

How to compare two MemoryStream with FluentAssertions

Using FluentAssertion 3.1.229, how do you compare the content of two distinct MemoryStream? Writing actualStream.Should().Be(expectedStream); yields the following error: System.IO.MemoryStream { CanRead = True CanSeek = True CanTimeout =…
dstj
  • 4,800
  • 2
  • 39
  • 61
5
votes
2 answers

In FluentAssertions, why is Should a method instead of a property?

In FluentAssertions, you can make various claims in various formats. x.Should().BeEquivalentTo(y); x.ShouldBeEquivalentTo(y); are both valid assertions. Why is Should a method and not a property? I haven't seen any examples in which Should takes a…
Luke Willis
  • 8,429
  • 4
  • 46
  • 79
5
votes
3 answers

Fluent Assertions compare string to Guid

I am trying to find the most fluent manner to assert that a certain string is a valid Guid. iterTags.GUID is a string. My first attempt ended in error because stringdoes not implement Guid. Okay, I saw it coming as it was a shot in the…
Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
5
votes
1 answer

How to test nested collections with FluentAssertions

I have the following spec BidirectionalGraph Fixture = new BidirectionalGraph(); public void VerticesShouldBeAbleToAssociateMultipleEdges() { int a = 0; int b = 1; int c = 2; Fixture.AddEdge(a, b); Fixture.AddEdge(b, c); …
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
4
votes
1 answer

Compare objects where some props have different formats

What is the best way to compare all properties of two objects where some of them have different formats (e.g. DateTime in one and DateTime.ToString() with custom format in other)? I was able to do that by using 2…
the_joric
  • 11,986
  • 6
  • 36
  • 57
4
votes
1 answer

Providing an extension to FluentAssertions

Because I have some angles, I would like to check for an angle modulus 360°: double angle = 0; double expectedAngle = 360; angle.Should().BeApproximatelyModulus360(expectedAngle, 0.01); I have written an extension to the Fluent…
Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53
4
votes
2 answers

How to get the exception object after asserting?

For example, I have the following code in my unit test. Action act = () => subject.Foo2("Hello"); act.Should().Throw() After the assertion, I want to run a couple more steps of processing on the thrown exception and…
HappyTown
  • 6,036
  • 8
  • 38
  • 51
4
votes
1 answer

How can I check if an object inherits from another class using FluentAssertions?

I would like to know how can I check if an object inherits from another class using Fluent Assertions? I know I can do that with xUnit using IsAssignableFrom, like so: [Fact] public void CreateBossEnemy() { //arrange EnemyFactory sut = new…
Rogerio Schmitt
  • 1,055
  • 1
  • 15
  • 35
4
votes
1 answer

Any possibility to override default FluentAsserions message?

Is there any possibility to override default messages of FluentAssertions. Sometimes I just want my custom message to be print as the result of failed test. So far I haven't found any solution for this but maybe I missed…
Gondil
  • 787
  • 3
  • 9
  • 28
4
votes
1 answer

FluentAssertions: How to compare two collections using a custom comparison on each pair of elements?

Given the following inputs: var customers = new[] { new Customer { Name = "John", Age = 42 }, new Customer { Name = "Mary", Age = 43 } }; var employees = new[] { new Employee { FirstName = "John", Age = 42 }, new Employee { FirstName…
KnorxThieus
  • 567
  • 8
  • 17
4
votes
1 answer

FluentAssertions fails with struct with enum but not class

I have a nested class and FluentAssertions can assert them. Then I change class to struct and the test fails. ( If I change IEnumerable MyItems { get; set; } to ItemStruct MyItem { get; set; } the comparison succeeds in both cases. So…
LosManos
  • 7,195
  • 6
  • 56
  • 107
4
votes
1 answer

Fluent Assertions Should().Should().BeEquivalentTo ignores excluded member

I am creating two objects of the same type in an integration test, but trying to exclude autoincremented members generated on object creation. I successfully exclude the RecordId and Number property, but the equivalence test fails on the Id…
Gnus
  • 41
  • 1
  • 3