Questions tagged [shouldly]

Shouldly is an assertion framework which focuses on giving great error messages when the assertion fails while being simple and terse.

Resources:

18 questions
60
votes
1 answer

Testing Exception Messages with Shouldly

Is there a way to test the exception messages with shouldly? An example: public class MyException: Exception{ } The method to be tested: public class ClassUnderTest { public void DoSomething() { throw new MyException("Message"); …
Steffen
  • 2,381
  • 4
  • 20
  • 33
22
votes
2 answers

How does the Shouldly assertion library know the expression the assertion was applied to?

The Shouldly assertion library for .NET somehow knows what expression the assertion method was called on so it is able to display it into the message. I tried to find out how it works but got lost in the source code. I suspect it looks into the…
Stilgar
  • 22,354
  • 14
  • 64
  • 101
8
votes
1 answer

How can I have custom asserts with Shouldly and maintain the call-site-specific assertion messages?

I'm using the excellent Shouldly library in my xUnit tests and I'm finding myself using the set sequence of assertions in different tests, so I'm combining them into new assertion extension methods - but when I do this I lose Shouldly's contextual…
Dai
  • 141,631
  • 28
  • 261
  • 374
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
1 answer

Should throw unit test fails but saga handler code does throw an unrecoverable exception

In my test constructor I set up my saga: public When_Testing_My_Saga() { _mySaga = new MySaga { Data = new MySaga.MySagaData() }; } My test asserts that a failure is thrown on not receiving vital data: [Fact] public void…
Spikee
  • 3,967
  • 7
  • 35
  • 68
3
votes
1 answer

C# in my unit test i want to see if it is null or an empty string

I'm using Machine spec / Shouldly and testing a mapping profile (mundane work). The mapping expectations are sometimes inconsistent. Sometimes I am seeing an "" explicit empty string get returned and sometimes it's a null value. Since we're mapping…
mche
  • 616
  • 10
  • 16
3
votes
2 answers

How to test int equality with ShouldBe

I am running c# tests using ShouldBe and I have this code: int x = 3; int y = 3; x.ShouldBeSameAs(y); Problem is it throws exception: An exception of type 'Shouldly.ShouldAssertException' occurred in Shouldly.dll but was not handled in user…
Uros
  • 2,099
  • 7
  • 23
  • 50
2
votes
0 answers

Does Shouldly have a statement like Assert.Fail()?

Most unit testing frameworks have a method like Fail, which unconditionally throws an assertion exception. Does Shouldly have this kind of instruction?
Pawel Gorczynski
  • 1,227
  • 1
  • 15
  • 17
1
vote
1 answer

Dictionary ShouldContainKeyAndValue fails unit test despite finding the expected value

I wrote this unit test using XUnit and Shouldly. Please note that I replaced parts of the namespace, variable names, properties etc, as the customer is a large public organization. [Fact] public async void…
RonRonDK
  • 425
  • 6
  • 22
1
vote
2 answers

Assert duplicate element in unit testing

How can I to make a unit test check that a list of object not contains a duplicate element based on some properties. Here is what I tried to do: [Fact] public void RecupererReferentielContactClient_CasNominal_ResultOk() { // Arange var…
ucef
  • 557
  • 3
  • 10
  • 27
1
vote
3 answers

Test two conditions with shouldly

I have a property in a class public class User { public string FiscalCode {get; set;} } And i want test the property fiscal code with two condition. The test is ok if fiscalcode is null or fiscalcode is verified by a method public bool…
user3401335
  • 2,335
  • 2
  • 19
  • 32
1
vote
0 answers

Can't use FluentAssertions/Shouldly

I wanted to use FluentAssertions or Shouldly so I added packageReference like this:
Mate
  • 192
  • 2
  • 18
0
votes
1 answer

How to assert the date format of a string using Shouldly and c#

I would like to check that the string from a textbox is of format "dd-MMM-yyyy" e.g. 14-Mar-2023 I have written the following code but I get an error string date =…
RShome
  • 489
  • 2
  • 11
  • 35
0
votes
1 answer

How to assert sending an email in unit testing

I have a method to send an email. How can I write a unit test to verify this method. I'm using MS test with shouldly public async Task SendEmail(string Id, string exception) { SendEmailModel sendEmailmodel = new…
Kumara
  • 1
  • 1
0
votes
1 answer

Visual Studio - disable stepping into source code of module (Shouldly)

I am using a an assertion framework called Shouldly for C#. The code looks like this: [Fact] public void SimpleTest() { false.ShouldBe(true); } This is the same as: [Fact] public void SimpleTest() { Assert.True(false); } However, when I…
harvzor
  • 2,832
  • 1
  • 22
  • 40
1
2