2008-07-08

Doing BDD, when expecting an exception

I really like the look of my tests specifications, they read almost like plain English.

After fumbling around for a while, I finally get how to specify an expected exception.

namespace Specs_for_Dictionary
{
    [TestFixture]
    public class When_adding_two_items_with_the_same_key
    {
        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void Should_throw_exception()
        {
            const string key = "UniqueKey";
            Dictionary dictionary = new Dictionary();
            dictionary.Add(key, new object());
            dictionary.Add(key, new object());
        }
    }
}

There is no Setup/Init method. Usually have one, that satisfies the "When_operation_on_state" class name. So that I can reuse the same specification context, for multiple expectations.

But then it hit me, that there can only be one "Should_expectation" method name, because the given "When_operation_on_state" always throws the exception.

No comments: