How to display an It approval statement in MSpec

We use MSpec for unit tests after MbUnit was previously used.

I'm used to what I can say

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

in MbUnit, that is, adding a message to the statement that is displayed if it fails.

I can not find the corresponding functionality in MSpec. I am testing some XML validations and, if that fails, I want to report a 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 message to the test output if ShouldBeTrue() fails.

Is it possible?

+7
source share
2 answers

Looking at the source for MSpec, no. Extension methods do not accept the string parameter for the message.

You can trivially add functionality yourself, in terms of code for writing; the code is in machine.specifications / Source / Machine.Specifications / ExtensionMethods.cs . I do not know how difficult it is to do it.

For example, you can create Overloads ShouldBeFalse and ShouldBeTrue as follows:

 [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); } 
+6
source

There is currently no infrastructure for this, but, like Matt, I would suggest implementing my own “reports” on the version of the built-in MSpec claims library (possibly in the <Product>.ForTesting class library).

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

+1
source

All Articles