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
4
votes
2 answers

invoking an async task on fluentassertion

probably a simple one, but cant get it to work; i've changed the signature one on the methods to Task On my unit tests i am using fluent assertions. but cant get this to work: _catalogVehicleMapper .Invoking(m =>…
Roelant M
  • 1,581
  • 1
  • 13
  • 28
4
votes
1 answer

FluentAssertions check that object fields not equal?

What is the best way in FluentAssertions to check that two objects are not equivalent, if the class does not implement Equals and I want to do field-by-field comparison with reflection? Basically I want the opposite of Should().BeEquivalentTo(),…
Samer Adra
  • 683
  • 7
  • 16
4
votes
1 answer

FluentAssertions - Should().BeEquivalentTo() when properties are of different type

How to compare objects that have a property with the same name but different type? public class A { public Guid Id { get; set; } } public class B { public string Id { get; set; } } public static B Map(A a){ return new B { Id =…
Anton Georgiev
  • 610
  • 6
  • 14
4
votes
2 answers

How to compare two collections that vary by properties using Fluent Assertion?

I have public class RuleInfo which is created from internal class Rule. private static RuleInfo CreateRuleInfo(Rule r) { return new RuleInfo { RuleCode = r.RuleId, DisplayName = r.RuleCode, Description =…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
4
votes
3 answers

Specflow Contextual Assertions

We use Specflow and currently, most of our assertions are done using Fluent Assertions, ie look something like: myval.Should().NotBe(null) All is working well, however there are some cases in which we want the same code to sometimes Assert…
Clive Galway
  • 482
  • 4
  • 13
4
votes
1 answer

Ignore internal properties in ShouldBeEquivalentTo

Is there a way to ignore internal properties of a class when doing ShouldBeEquivalentTo? For example, in the class below I want to exclude the MetaData property from the object graph comparison. public class SomeObject { Public string…
Lejdholt
  • 532
  • 7
  • 21
4
votes
1 answer

Why does nameof() give an ambiguous invocation warning in a Linq expression, but not when I use the same value as a string?

I'm getting a compiler warning that started happening when I upgraded to FluentAssertions 4.2.2. In the following code, if I call EndsWith(nameof(x)), I get an ambiguous invocation warning. If instead I define var foo = nameof(x) and call…
herky
  • 43
  • 5
4
votes
2 answers

Performing assertions on multiple properties of a Class

I know from the docs I can do this ... result.Should().BeOfType().Which.Property1.Should().Be("String") Is there a way I can test multiple properties, in a way similar…
Stuart Hemming
  • 1,553
  • 2
  • 21
  • 44
4
votes
1 answer

How can I compare null and string.Empty (or "") in fluent assertions?

I have two objects of the same type, the type has a string field, in the first object the value is null, in the second one the value is "", how can I force fluent assesrtions to assume that this is correct? Assesrtion itself:…
HardLuck
  • 1,497
  • 1
  • 22
  • 43
4
votes
1 answer

FluentAssertions: ShouldBeEquivalentTo method still invokes Object.Equals()?

I have a class, let's call it Foo, that is a value type and hence overrides the Equals/GetHashCode() methods. In a separate test fixture, I want to assert that all the properties on Foo have been set properly, not just the properties used for…
Mitch A
  • 2,050
  • 1
  • 21
  • 41
3
votes
1 answer

Latest nuget packge for FluentAssertions missing System.Xml references

FluentAssertions nuget package for 1.7 seems to be missing the references for System.Xml and System.Xml.Linq Is anyone else experiencing this problem?
628426
  • 373
  • 3
  • 14
3
votes
0 answers

How to choose between Shouldly and FluentAssertions?

What are the differences (if any) between Shouldly and FluentAssertions? Can either do anything that the other one can't? Or are they functionally the same with just a different syntax? The only other post I can find on this is from 2013. It's now…
GoldieLocks
  • 845
  • 7
  • 22
3
votes
2 answers

Fluent assertion Should().ThrowExactlyAsync should fail for derived types, but it doesn't

The following Func delegate throws an ArgumentNullException: Func act = async () => await _someService .someMethod(1, 2, 3, 4); Using Fluent assertions, the assertion: act.Should().ThrowExactlyAsync(); Should…
anastaciu
  • 23,467
  • 7
  • 28
  • 53
3
votes
2 answers

Asserting async exception and paramName with FluentAssertions

I'm using FluentAssertions. For a sync test, I can write this: action.Should().Throw().And.ParamName.Should().Be("foo"); For an async test, I do this: await action.Should().ThrowAsync(); Is there a…
lonix
  • 14,255
  • 23
  • 85
  • 176
3
votes
1 answer

How to use `Which` in FluentAssertions?

I'm using fluent assertions and I have this test: result.Should().NotBeNull(); result.Link.Should().Equals("https://someinvoiceurl.com"); which works fine but when I try this …
geckos
  • 5,687
  • 1
  • 41
  • 53