A couple of days ago Hadi Hariri posted his set of MSpec (Machine.Specifications) templates for ReSharper. ReSharper’s templating system helps you type less repeated code. On top of that, ReSharper templates are much richer when compared to what’s built into Visual Studio. Plus, you edit them with a decent editor instead of hacking XML files.
Like Hadi, I also created a couple of templates specific to MSpec over the course of the last year or so and found them often to reduce the amount of text I have to write. ReSharper Templates are divided into three categories, with at least one MSpec template in each.
foo
|
Basically, this is just a new C# file with a single MSpec context in it.
using System; using Machine.Specifications; namespace ClassLibrary1 { [Subject(typeof(Type))] public class When_Context { Establish context = () => { | }; Because of = () => { }; It should_ = () => { }; } }
Live Templates provide expansion of keyword-like identifiers. For example cw (Tab) will expand to Console.WriteLine();
cw (Tab)
Console.WriteLine();
spec
est
Establish context = () => { | };
bec
Because of = () => { | };
it
It should_observation = () => { | };
fail
It should_fail = () => Exception.ShouldNotBeNull(); static Exception Exception;
l
() => | ;
Only valid for assignments. For example:
var x = l (Tab) var x = () => | ;
ll
() => { | };
Surround Templates are useful when you want to wrap a block of code with other code, for example, an if statement (this is one that’s built-in).
if
Unit testing frameworks almost always have means to assert that are particular test should fail with a specific exception, for example by marking the test method with the ExpectedExceptionAttribute.
ExpectedExceptionAttribute
The MSpec way of handling/expecting exceptions is to surround the code in Because with Catch.Exception:
Because
Catch.Exception
public class When_a_negative_amount_is_deducted { static Exception Exception; static Account Account; Establish context = () => { Account = new Account(); }; Because of = () => { Exception = Catch.Exception(() => Account.Deduct(-1)); }; It should_fail = () => Exception.ShouldNotBeNull(); }
There’s a surround template named Catch.Exception that we can make wrap the call to Account.Deduct:
Account.Deduct
Account
Establish
public class When_a_negative_amount_is_deducted { // Account field and Establish cut for brevity. Because of = () => { Account.Deduct(-1); }; }
public class When_a_negative_amount_is_deducted { // Account field and Establish cut for brevity. Because of = () => { Exception = Catch.Exception(() => { Account.Deduct(-1); }); }; }
(End)
(Tab)
public class When_a_negative_amount_is_deducted { // Account field and Establish cut for brevity. Because of = () => { Exception = Catch.Exception(() => { Account.Deduct(-1); }); }; fail(Tab) }
public class When_a_negative_amount_is_deducted { // Account field and Establish cut for brevity. Because of = () => { Exception = Catch.Exception(() => { Account.Deduct(-1); }); }; It should_fail = () => Exception.ShouldNotBeNull(); static Exception Exception; }
Download Machine.Specifications Templates For ReSharper
a@href@title, blockquote@cite, em, strike, strong, sub, sup, u