Contact

admin

About Me · Send mail to the author(s) E-mail · Twitter

At GROSSWEBER we practice what we preach. We offer trainings for modern software technologies like Behavior Driven Development, Clean Code and Git. Our staff is fluent in a variety of languages, including English.

Feed Icon

Tags

Open Source Projects

Archives

Blogs of friends

Now playing [?]

  1. Marilyn Manson – Slutgarden
  2. Marilyn Manson – Ka-Boom Ka-Boom
  3. Marilyn Manson – (s)AINT
  4. Marilyn Manson – Use Your Fist and Not Your Mouth
  5. Marilyn Manson – Doll-Dagga Buzz-Buzz Ziggety-Zag
Audioscrobbler/Last.fm

ClustrMap

Machine.Specifications Templates For ReSharper

Posted in BDD | MSpec | ReSharper at Wednesday, March 03, 2010 2:56 PM W. Europe Standard Time

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.

Legend

  • foo denotes an editable part of the template
  • | denotes where the cursor will be put upon expansion

File Template

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 (a.k.a. Snippets)

Live Templates provide expansion of keyword-like identifiers. For example cw (Tab) will expand to Console.WriteLine();

Name Expanded
spec A new context, similar to what the File Template above creates.
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 () => { | }; Only valid for assignments.

Surround Templates

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).

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.

The MSpec way of handling/expecting exceptions is to surround the code in Because with 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:

  1. Create the context with just the Account field, Establish and the Because. Select the highlighted code.
    public class When_a_negative_amount_is_deducted
    {
      // Account field and Establish cut for brevity.
    
      Because of =
        () => { Account.Deduct(-1); };
    }
  2. Press the shortcut for ReSharper | Edit | Surround With Template, select "Catch.Exception" from the list of available templates.
    public class When_a_negative_amount_is_deducted
    {
      // Account field and Establish cut for brevity.
    
      Because of =
        () => { Exception = Catch.Exception(() => { Account.Deduct(-1); }); };
    }
  3. Navigate out of the Because field, for example by pressing the (End) key. Type fail (a Live Template, see above) and press (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)
    }
  4. Marvel at the amount of code you didn't have to write.
    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

Sunday, June 06, 2010 8:34:35 AM (W. Europe Daylight Time, UTC+02:00)
Great stuff! I've made great use of your templates already!
Thanks a bunch for sharing
Monday, July 12, 2010 6:41:54 PM (W. Europe Daylight Time, UTC+02:00)
Excellent stuff!! Thanks for sharing this.

I am currently facing an issue with running Machine Specification tests using Resharper 5.0. and was wondering if you could help. The individual tests appear as greyed out in the Reshaper Test Runner and I am also not able to debug them using the Resharper. Although none of the tests is running but I still see the green bar as if all the tests were successfully passed. Any ideas as to how I can get this working?
Agha
Monday, July 12, 2010 7:15:56 PM (W. Europe Daylight Time, UTC+02:00)
Please make sure the MSpec runner DLL matches the MSpec version used by your project. In 95% of all cases this resolves the issue with grayed-out contexts.
All comments require the approval of the site owner before being displayed.
(will show your gravatar icon)
 
[Captcha]Enter the code shown (prevents robots):

Live Comment Preview