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.
I am trying to compare a Json file with some values using the following method:
offerte is of the type Newtsonsoft.Json.Linq.JObject. I also tried making offerte a dynamic type but it still sees it as ...
I have a unit test configured using SpecFlow v3 and using FluentAssertions library for assertions.
I have a few tests (queries against mocked dbset to return list of objects) that pretty much are the ...
asked Mar 11 at 1:25
Bmoe
54866 silver badges2121 bronze badges
I want to check if all endpoints of my ASP.NET Core API controllers have an attribute that looks line this:
[SwaggerResponse(HttpStatusCode.OK,typeof(*different types*))]
I used xUnit and Fluent ...
I've created the following very simple test which is a simplification of a scenario I've come across in the wild.
var actual = new JObject
{
{"prop1" , "1"},
{"prop2&...
I'm generating a C# solution using another project.
The generated solution contains multiple classes and a unit test file for each class.
A unit test creates two instances of a class and compares them....
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 ...
asked Feb 24 at 21:03
Emmanuel DURIN
4,40022 gold badges2424 silver badges4545 bronze badges
I started to use FluentAssertions recently, which supposed to have this powerful object graph comparison feature.
I'm trying to do the simplest thing imaginable: compare the properties of an Address ...
asked Feb 2 at 10:02
Leaky
1,65422 gold badges1616 silver badges2424 bronze badges
case I am working on is - what is best solution to compare two objects with properties but excluding members with null values.
Ex.
objectA.prop1 = "value1";
objectA.prop2 = "value2"...
Is it possible to force fluent assertions to pass Should().Be() for comparison between null and empty string? Or maybe this can be done somehow with BeEquivalentTo?
Example:
text1.Should().Be(text2);
...
Is there a possibility to get back a list of failed and passed objects in Fluent Assertions?
E.g.
var results = actual.Should().BeEquivalentTo(tc.Expected);
I am trying to get all of the results so ...
asked Dec 7 '20 at 8:10
YoungDad
37511 gold badge22 silver badges1010 bronze badges
I want to prepend a component name and the type information before any errors are shown. Typically I am asserting custom types. I want to know which types are being compared before seeing the rest of ...
asked Nov 27 '20 at 8:26
ceco
1,51633 gold badges1515 silver badges2323 bronze badges
I have a class called HandRank which has an Id property. The Id property is generated by my database.
public class HandRank
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
...
asked Nov 10 '20 at 22:16
I try to make a test using FluentAssertion pass but i get the famous "System.InvalidOperationException : No members were found for comparison" and i don't know how i can make it pass in this ...
asked Nov 10 '20 at 15:10
I've been messing around with some of c#9's new features, and I've run into something that's less than fun. If I try to use Should().BeEquivalentTo() on a record with a DateTimeOffset, setting the ...
asked Oct 16 '20 at 22:46
I'm comparing two simple dictionaries. I add items to the dictionaries in opposite order. I'm using the WithStrictOrdering option in FluentAssertions but this test passes and I think it should fail:
...
asked Oct 15 '20 at 16:31
NJS
19611 silver badge1010 bronze badges
I want to test that an XML element's value contains a particular string. [The code below is clearly highly contrived and not from the real codebase I am working on]. How to check with Fluent ...
asked Oct 2 '20 at 14:22
Adrian S
42766 silver badges1414 bronze badges
I have a simple class with a property of type object, which can contain JSON built from different sources
public class SimpleClass
{
public string Id { get; set; }
public object JSONData { get;...
asked Sep 25 '20 at 5:16
NJS
19611 silver badge1010 bronze badges
I'm using a library called Optional (https://github.com/nlkl/Optional) that allows the "maybe" abstraction that is common to functional languages.
The library is awesome, but I'm facing a ...
asked Sep 16 '20 at 11:54
SuperJMN
10.2k99 gold badges6464 silver badges143143 bronze badges
Description
I'm iterating over 2 lists
I want to compare the 2 values at each point in the list
I want the value to be within 0.1% of each other
Complete minimal example reproducing the issue
var ...
asked Sep 14 '20 at 10:55
I have to write a test which compares attributenames and elementnames of two xml documents.
The values should be ignored.
The test should pass if I would compare these two files:
<person age="...
I want my test to proceed next line after the assert fails. I tried "try catch block" but tests are not failing. Do we have anything that can fix this issue in fluent assertion [https://...
asked Sep 10 '20 at 14:52
I'm trying to determine the result of an assertion in order to use the output for reporting, something like this:
ProblemResolution().Should().Match("easy");
Logger(Assertion.Result());
I ...
I'm going to do some unit tests and I am struggling with a MessageBox. I have a MessageBox that is showing a text and an "Ok" button in my code. When I trying to unit test the method that ...
I have a number of nested complex objects that I'm attempting to compare with Fluent Assertions with the following code:
restResponse.Should().BeEquivalentTo(mappedSoapResponse, options =>
...
asked Jul 30 '20 at 9:22
m.edmondson
28.4k2626 gold badges113113 silver badges191191 bronze badges
I'm writing a C# unit test in Visual Studio 2019. I'm using Xunit with FluentAssertion.
The two main lines of code (for illustrative purposes) in one of the tests are:
Action a = () => new ...
asked Jul 13 '20 at 15:50
millie
2,45288 gold badges3333 silver badges5353 bronze badges
This seems like a long shot...
I am building a test harness for manual testing (for my QA Team). It runs in a console application and can output some level of smart data, but nothing so automatic as ...
asked Jun 30 '20 at 19:36
Vaccano
69.5k126126 gold badges403403 silver badges739739 bronze badges
I used FluentAssertions; it's great! :) I particularly like using the .Invoking().Should().Throw<Exception>() pattern it provides.
I wrote this line in my test:
myObject.Invoking(r => r....
asked Jun 26 '20 at 18:04
Brondahl
5,06011 gold badge2424 silver badges4545 bronze badges
Sometimes I have to assert that two lists have the same items. With fluent assertions this can be done like this:
class MyObject { public string MyString {get; set;} }
var o1 = new MyObject { ...
asked May 28 '20 at 16:26
What is the difference between:
subject.Should().Be(expected)
subject.Should().Equals(expected)
I always use Be(), but I now have a testcase where Be() gives a different result, then Equals(). My ...
For example, I have the following code in my unit test.
Action act = () => subject.Foo2("Hello");
act.Should().Throw<InvalidOperationException>()
After the assertion, I want to run a ...
asked Apr 26 '20 at 22:48
HappyTown
4,94066 gold badges3030 silver badges4343 bronze badges
My question title brings general problems when standard .NET exceptions are mostly meaningless with stacktrace or extra information like
Sequence contains more than one matching element
I am too ...
asked Apr 17 '20 at 14:17
do you know if fluent assertion framework can compare html, or saw an extension that does it? I know it doesn't have any problem with the JSON, XML formats. But I didn't find anything regardless html. ...
asked Apr 16 '20 at 11:10
Irdis
71077 silver badges1414 bronze badges
I got two objects with the same data, double, long, string my problem is in object A all strings are upper case and in object B upper and lower.
objectA.Should.Should().BeEquivalentTo(objectB);
The ...
asked Mar 23 '20 at 21:33
The Fluent Assertions library puts quite some emphasis on its capabilities of comparing object graphs by means of the Should().BeEquivalentTo method and related methods. As the documentation points ...
asked Feb 28 '20 at 14:22
F-H
42911 gold badge44 silver badges1313 bronze badges
Why can't I do the following with FluentAssertions, using the 'And' property?
SomeObject.Should()
.BeAssignableTo<OtherObject>()
.And
.SomeStringProperty.Should().StartWith("whatever");...
asked Feb 25 '20 at 14:30
stonedauwg
1,1181010 silver badges2828 bronze badges
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;...
asked Feb 25 '20 at 13:25
I'm trying to write test checking if JSON converter corectly deserialize input to my custom list
[TestMethod]
public void JSONInput_Changed()
{
List<PointOnChart> ...
asked Feb 19 '20 at 11:03
I am trying to create some Assertion classes using the FluentAssertions library. This is the Assertion code:
public AndConstraint<MyTaskAssertions> Work(string because = "", params object[] ...
asked Feb 16 '20 at 21:30
I am using a client to interact with CloudMQTT API. I am trying to create a user but after trying the code provided below, I was not able to create a user. When using the code provided within the ...
asked Jan 15 '20 at 10:30
Barrosy
1,33322 gold badges1717 silver badges4242 bronze badges
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 ...
asked Dec 19 '19 at 20:38
Is it possible to continue after a failed Assertion in FluentAssertions?
I had some assertions which are no show stoppers and should only be reported but not failing the Test run.
[TestClass]
public ...
asked Dec 16 '19 at 8:19
Christian
72155 silver badges2020 bronze badges
How can I reuse the FluentAssertions' build-in object graph comparison in my custom assertions class, so that I can use the options parameter to scope the comparison.
Here is an example. There is a ...
asked Dec 15 '19 at 23:32
Would it be possible to test a function with returns void and only writes a line to the console like the following using FluentAssertion?
static void WriteLine()
{
Console.WriteLine("It works!");
...
asked Dec 10 '19 at 14:04
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 ...
asked Dec 5 '19 at 10:19
Gondil
71711 gold badge99 silver badges2222 bronze badges
There is official example how to create CustomAssertion at FluentAssertions docs, however my attempt to apply it fails. Here's the code:
public abstract class BaseTest
{
public List<int> ...
asked Nov 29 '19 at 13:35
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 ...
asked Nov 29 '19 at 10:03
I'm writing a test that verifies if my controller calls my query with expected query arguments. This is my query class:
public class GetProducts : IRequest<IEnumerable<Product>>
{
...
asked Nov 26 '19 at 12:17
I want to test a method returning collection using fluent-assertions to ensure that returned collection is a child of other collection, I tried fatherCollection.Should().Contain(childCollection); but ...
asked Nov 21 '19 at 3:50
jimbo R
22711 gold badge33 silver badges1414 bronze badges
The Fluent Assertions framework provides a plethora of collection-related assertions.
For checking whether a given collection contains an element that satisfies a specific condition, I can use Should(...
asked Nov 8 '19 at 9:51
F-H
42911 gold badge44 silver badges1313 bronze badges
I have simple class, for example
class Person {
public string Name;
public string LastName;
public Person Parent;
public static int IdCounter = 0;
public int Id;
public Person(...