Try fast search NHibernate

12 July 2009

NUnitEx 1.0.2

NUnitEx 1.0.2 was released today.

The new release is targeting NUnit2.5.1 released few days ago.

The new NUnitEx include some new fluent assertions and one extension method useful in tests.

FieldValue<T>

Allow access to a private field where a public property is not available.

[Test]
public void SelectAndUpdateStringContainCustomWhere()
{
const string customWhere = "table_name='second'";
var dialect = new MsSql2005Dialect();
var tg = new TableGenerator();
tg.Configure(NHibernateUtil.Int64,
new Dictionary<string, string> { { "where", customWhere } }, dialect);
    tg.FieldValue<string>("query").Should().Contain(customWhere);
tg.FieldValue<string>("updateSql").Should().Contain(customWhere);
}

Guess where was needed ;)

System.Type constraints
private class B {}

[Serializable]
private class D1 : B { }

private class D2 : D1 { }
var typeToTest = typeof(D1);

typeToTest.Should().Be.SubClassOf<B>();
typeToTest.Should().Be.AssignableFrom<D2>();
typeToTest.Should().Have.Attribute<SerializableAttribute>();
String Match for custom Regex
var re = new Regex("a.b", RegexOptions.Singleline | RegexOptions.IgnoreCase);
"a\nB".Should().Match(re);

Full Syntax Overview

The full syntax overview is available in the NUnitEx wiki.

Consideration

Have a look to this NUnit assertion:

Assert.That(stringToTest, Is.EqualTo("a String Value").IgnoreCase);

What kind of comparison is used ? What mean “IgnoreCase” ?

One Microsoft recommendation about string usage is:

DON'T: Use overloads for string operations that don't explicitly or implicitly specify the string comparison mechanism.

Under the cover the NUnit assertion is equivalent to :

stringToTest.ToLower().Equals("a String Value".ToLower());

As you know the ToLower method is not enough in some languages and the default assertion is obscuring it.

For this reason I prefer:

stringToTest.ToLowerInvariant().Should().Be.EqualTo("a string value");

3 comments:

  1. Hi Fabio excellent work as usual. For type constraints will be very nice to have Be.AssignableTo<..>() (or Implements<>) when working with proxies and "additional interfaces".
    proxy.GetType().Should().Be.AssignableTo<ISomeInterface>()

    ReplyDelete
  2. Sure, can you add it in the issue tracker ?
    Thanks.

    ReplyDelete
  3. Updated. Thanks :)

    ReplyDelete