Step by Step to Using MSpec (Machine.Specifications) with ReSharper

Whilst researching using MSpec with ReSharper I found it difficult to find all the resources I needed in one place. This is an attempt to condense everything into that one place and facilitate those seeking to accomplish the same task.

Step 1 – Git It:

First thing’s first, grab the latest Machine Spec source from github.

$ git clone git://github.com/machine/machine.specifications.git mspec

Step 2 – Build It:

Next, open it up in Visual Studio, set it to build in release mode and build it. Now the binaries will be ready for you.

Step 3 – Setup ReSharper:

Now we need to setup ReSharper to be able to utilize the MSpec framework and run the tests in ReSharper’s test runner. To do this we need to add a plugins directory to the “JetBrainsReSharperv4.5Bin” directory or the where ever the bin directory for your ReSharper is located. In the Plugins create a Machine.Specifications directory, so you should now have a path similar to; “JetBrainsReSharperv4.5BinPluginsMachine.Specifications”. Place the following dlls in the newly created folder: Machine.Specifications.ReSharperRunner.4.5.dll and Machine.Specifications.dll.

Step 4 – Write some Specifications:

Coolio, now to test some behaviors, the dlls needed in our test project; Machine.Specifications.dll, Machine.Specifications.NUnit.dll or Machine.Specifications.XUnit.dll, and the appropriate test framework dll.

Let’s take a look at a couple of examples to get used to the syntax. The most common keywords you want to pay attention to are Subject, Establish, Because and It. Declare the Subject of your Spec, Establish a context of the spec, Because x occurs, It should do something. For more complex scenarios you can use the keyword, Behaves_like and the Behaviors attribute which allows you to define complex behaviors. If you need to perform some cleanup use the Cleanup keyword.

Now for a couple of simple contrived examples…

This first specification looks at adding two numbers:

[Subject("adding two operands")]
public class when_adding_two_operands
{
	static decimal value;

	Establish context = () =>
		value = 0m;

	Because of = () =>
		value = new Operator().Add(42.0m, 42.0m);

	It should_add_both_operands = () =>
		value.ShouldEqual(84.0m);
}

The second specification looks at adding multiple numbers:

[Subject("adding multiple operands")]
public class when_adding_multiple_operands
{
	static decimal value;

	Establish context = () =>
		value = 0m;

	Because of = () =>
		value = new Operator().Add(42m, 42m, 42m);

	It should_add_all_operands = () =>
		value.ShouldEqual(126m);
}

The code being tested:

public class Operator
{
	public decimal Add(decimal firstOperand, decimal secondOperand)
	{
		return firstOperand + secondOperand;
	}

	public decimal Add(params decimal[] operands)
	{
		decimal value = 0m;

		foreach (var operand in operands)
		{
			value += operand;
		}

		return value;
	}
}

Step 5 – Create Templates to Improve Your Efficiency:

Using ReSharper templates is a good way to improve your spec writing efficiency, the following are templates I have been using.

This first one is for your normal behaviors:

[Subject("$subject$")]
public class when_$when$
{
	Establish context =()=>	{};

	Because of =()=> {};		
	
	It should_$should$ =()=> $END$;
}

This one’s for assertions by themselves:

It should_$should$ =()=> $END$;

Step 6 – Run the Report

The report generated is pretty much the exact same as the SpecUnit report. The easiest thing to do is place the following MSpec binaries and file in the directory where your test binaries are placed; CommandLine.dll, Machine.Specifications.ConsoleRunner.exe, Machine.Specifications.dll and CommandLine.xml. The command to run the report goes a little something like:

machine.specifications.consolerunner --html <the location you want the html report stored> <your test dll name>

This is the report generated from the example specs:

Well, that’s about all for now, let me know if you have any questions.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Sean Biefeld

Hello all, my name is Sean Biefeld. I graduated from Baylor University with a BBA in Management Information Systems. I am currently working for McLane Advanced Technologies . The opinions expressed by me, however accurate they may be, are not necessarily those of my employer. I have been a developing software professionally since 2004. My primary development focus is on HTML, CSS, JavaScript and C#. I dabble in a little PHP and Ruby. This blog is a forum for me to post my experiences, ideas, rants, and thoughts as I traverse the mountain of life.
This entry was posted in BDD, Behavior Driven Design, Git, MSpec, ReSharper, Specifications, Unit Test. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.lostechies.com/members/bogardj/default.aspx bogardj

    One last thing you might consider is linking AutoHotKey and R#:

    http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/02/26/seamless-test-authoring-with-resharper-and-autohotkey.aspx

    You’ll never hit that pesky underscore key again.

  • http://www.lostechies.com/members/derick.bailey/default.aspx derick.bailey

    great post, yo. that’s about the best intro to MSpec I’ve seen and makes me more interested in trying it out again.

  • http://elegantcode.com Richard Cirerol

    I have been using MSpec for about a month now, and am becoming very comfortable with it. However, I haven’t dove into using the behaviors and behaves_like options…I am really interested in seeing some practical examples. Any chance on a follow-up post?

  • http://www.lostechies.com/members/seanbiefeld/default.aspx Sean Biefeld

    @bogardj cool I’ll look into autohotkey, at the moment I am just using a visual studio macro for underscores

    @derick.bailey glad you enjoyed, thanks

    @Richard Cirerol I also have not delved much into the behaves_like options but it’s on my list of things to do, if I can come up with some good examples, I’d be happy to post them

  • http://www.tjsolutions.nl Tom de Koning

    Also, make sure you run the reg file in the Testdriven.net directory. This will allow VS to pick up the tests with TD.net. (make sure you adjust the path to the td.net dll)

    I have some issues with the TDD flow when using MSPEC. Normally I use “Introduce variable” when asserting on the output of a property or method. Because the variable needs to be declared static in order to be seen in the It delegate this doesn’t work.

    Does anyone have a tip for this?

  • Denis

    Are MSpec “tests” integrated with Resharper TestRunner

  • http://www.tjsolutions.nl Tom de Koning

    btw, the location to the git repository is incorrect; it should be

    git://github.com/machine/machine.git =>

    git clone git://github.com/machine/machine.git mspec

  • http://www.blogcoward.com jdn

    Sorry, I’m missing something. How does this help with ReSharper? If I try to right-click and run unit tests on my already existing mspec specs after doing steps 1, 2, 3, all I get is the message “No unit tests found.”

    I’m sure I missed something, but not sure what.

  • http://www.lostechies.com/members/seanbiefeld/default.aspx Sean Biefeld

    @jdn
    If you had visual studio open at the time you set up resharper to handle mspecs, you will have to restart visual studio.

  • http://www.blogcoward.com jdn

    DOH!

    Thanks. That works.

  • http://rohancragg.co.uk Rohan Cragg

    *** This might help others with the report output HTML working. ***

    The assembly ‘Machine.Specifications.Reporting.dll’ is also required to be place in the test out put directory. For example, I added the following as a post-build step in my .csproj settings (i.e Project | Settings – Build Events):

    > machine.specifications.consolerunner –html $(TargetDir)mspec.rpt.html $(TargetDir)$(TargetFileName)

  • http://marcinobel.com Marcin Obel

    You can find more Resharper templates for MSpec here: http://marcinobel.com/index.php/resharpers-live-templates-for-mspec-bdd-framework/

  • http://marcinobel.com Marcin Obel

    Hi All,

    I have released an intaller for MSpec 3.0. You can download it from my blog: http://marcinobel.com/index.php/mspec-bdd-installer/
    Please, let me know if everything works fine.