Fluent Syntax for Assertion in msTest

Switching from Nunit to MsTest is quite simple, but the first thing I really miss with mstest was the lack of a fluent assertion syntax. Consider the assertion you must write if you want to check that a number is lesser than 100 or greater than 200

1
2
3
4
5
6
[TestMethod]
public void BaseAssertFluentWithSyntaxHelperLtOrGtRo()
{
    Int32 obj = 400;
    Assert.IsTrue(obj < 100 || obj > 200);
}

It is quite clear, but with fluent syntax you can write test like this

1
2
3
4
5
6
[TestMethod]
public void BaseAssertFluentWithSyntaxHelperLtOrGtR()
{
    Int32 obj = 400;
    FluentAssert.That(obj, Is.LesserThan(100).Or.GreaterThan(200));
}

I find it really clearer than the first form. The advantage of a fluent syntax is that you can read the assertion as if were English text. I’m actually writing an helper assembly to support fluent syntax with msTest, if someone is interested in I can publish in source form. For now I’m able to support basic syntax with operator precedence and parenthesis

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
[TestMethod]
public void TestBasic2()
{
    IConstraint c = Is.LesserThan(100) | Is.GreaterThan(200) & Is.GreaterThan(300);
    Assert.IsTrue(c.Validate(99));
}

[TestMethod]
public void TestBasicParenthesis()
{
    IConstraint c = (Is.LesserThan(100) | Is.GreaterThan(200)) & Is.GreaterThan(300);
    Assert.IsFalse(c.Validate(99));
}

As you can see the first test pass because the And operator ( expressed by & because you cannot overload &&) has higher precedence over the or operator. The second test pass because you can alter precedence with parenthesis.

Alk.