Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output a message with an It assertion in MSpec

We're using MSpec for unit tests after having previously used MbUnit.

I'm used to being able to say

Assert.IsTrue(status, "Status should be true");

in MbUnit, i.e. adding a message to the assertion which is output if it fails.

I can't find any corresponding functionality in MSpec. I'm testing that some XML validates and, if it fails, I want to report the validation error message. So my MSpec code looks like

string message;

bool isValid = ValidateXml(myXml, out message);

isValid.ShouldBeTrue();

But I want to be able to add the message to the test output if the ShouldBeTrue() fails.

Is this possible?

like image 333
Mark Pim Avatar asked Mar 30 '11 10:03

Mark Pim


People also ask

What is an assertion message?

From a test readability perspective, assertion messages are code comments. Instead of relying on them, refactor tests to be self-documenting. In terms of ease of diagnostics, a better alternative to assertion messages is: Making tests verify a single unit of behavior.

What is assertion in jest testing?

An assertion is a check that values meet certain conditions. In other words, if you use expect. assertions(5) the test will fail unless expect() is called at least 5 times. This is useful for async tests, but it's not the only way to handle asynchronicity, you can find other patterns in the Jest doc.

Why use Fluent assertions?

The main advantage of using Fluent Assertions is that your unit tests will be more readable and less error-prone. This is because Fluent Assertions provides many extension methods that make it easier to write assertions.


2 Answers

Looking at the source for MSpec, no. The extension methods do not take a string parameter for a message.

You could trivially add the functionality yourself, in terms of code to write; the code is in machine.specifications / Source / Machine.Specifications / ExtensionMethods.cs. I don't know how hard it is to build.

For example you could create overloads of ShouldBeFalse and ShouldBeTrue like so:

[AssertionMethod]
public static void ShouldBeFalse([AssertionCondition(AssertionConditionType.IS_FALSE)] this bool condition, string message)
{
  if (condition)
    throw new SpecificationException(message);
}

[AssertionMethod]
public static void ShouldBeTrue([AssertionCondition(AssertionConditionType.IS_TRUE)] this bool condition, string message)
{
  if (!condition)
    throw new SpecificationException(message);
}
like image 164
Matt Ellen Avatar answered Oct 11 '22 12:10

Matt Ellen


There's no infrastructure for this at the moment, but like Matt I would suggest implementing your own "reporting" assertions on top of MSpec's built-in assertion library (possibly in a <Product>.ForTesting class library).

In all other cases, the It field tells what should be observable, hence the missing message parameter.

like image 33
Alexander Groß Avatar answered Oct 11 '22 12:10

Alexander Groß