如何测试NUnit助手?

时间:2010-09-12 00:11:57

标签: unit-testing nunit

我在NUnit 2.5+之上构建了一个受RSpec启发的库,以提高我的测试可读性。源代码位于http://github.com/educobuci/SpecUnit

这个库允许我编写这样的测试:

[Test]
public void It_should_returns_0_for_all_gutter_game()
{
    var game = new Bowling();
    for (int i = 0; i < 10; i++)
        game.Hit(0);
    game.Score.Should(Be.Equal(0));
}

“Should”方法是所有Object的扩展方法,它基本上接收一个具有某些NUnit断言的Action:

public static class Be
{
    public static Action<T> Equal(T to)
    {
        return (target) => NUnit.Framework.Assert.AreEqual(target, to);
    }
} 

图书馆工作得很好但我没有对图书馆本身的测试...基本上因为我不知道如何测试它!

那么,我该如何测试呢?我的意思是,如何确保“object.Should(Be.Equal(object))”真正检查相等性?

1 个答案:

答案 0 :(得分:1)

[Test]
public void Be_Equal_action_throws_AssertionException_for_inequal_integers()
{
   var action = Be.Equal(0);
   bool raised = false;
   try
   {
       action(1);
   }
   catch (AssertionException)
   {
       raised = true;
   }
   Assert.IsTrue(raised, "No AssertionException was thrown");
}