Los Techies : Blogs about software and anything tech!

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 "JetBrains\ReSharper\v4.5\Bin" 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; "JetBrains\ReSharper\v4.5\Bin\Plugins\Machine.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.

Kick It on DotNetKicks.com
Posted Aug 25 2009, 09:34 PM by Sean Biefeld

Comments

Sanjeev Agarwal wrote Daily tech links for .net and related technologies - August 25-27, 2009
on 08-26-2009 7:17 AM

Daily tech links for .net and related technologies - August 25-27, 2009 Web Development Clean Web.Config

bogardj wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 08-26-2009 8:13 AM

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

www.lostechies.com/.../seamless-test-authoring-with-resharper-and-autohotkey.aspx

You'll never hit that pesky underscore key again.

derick.bailey wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 08-26-2009 9:34 AM

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

Richard Cirerol wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 08-26-2009 10:00 AM

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?

Sean Biefeld wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 08-26-2009 10:10 AM

@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

Tom de Koning wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 08-26-2009 1:16 PM

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?

Twitter Trackbacks for Step by Step to Using MSpec (Machine.Specifications) with ReSharper - Sean Biefeld's Blog - Los Techies : [lostechies.com] on Topsy.com wrote Twitter Trackbacks for Step by Step to Using MSpec (Machine.Specifications) with ReSharper - Sean Biefeld's Blog - Los Techies : [lostechies.com] on Topsy.com
on 08-26-2009 3:19 PM

Pingback from  Twitter Trackbacks for                 Step by Step to Using MSpec (Machine.Specifications) with ReSharper - Sean Biefeld's Blog - Los Techies :         [lostechies.com]        on Topsy.com

DotNetShoutout wrote Step by Step to Using MSpec (Machine.Specifications) with ReSharper - Sean Biefeld - Los Techies
on 08-27-2009 9:30 AM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Denis wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 08-27-2009 2:11 PM

Are MSpec "tests" integrated with Resharper TestRunner

Tom de Koning wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 08-28-2009 9:42 AM

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

jdn wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 09-06-2009 7:58 PM

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.

Sean Biefeld wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 09-07-2009 1:02 PM

@jdn

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

jdn wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 09-07-2009 8:02 PM

DOH!

Thanks.  That works.

Rohan Cragg wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 09-26-2009 4:52 AM

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

Howard Dierking wrote Using Latest NUnit Version With MSpec
on 11-08-2009 3:12 PM

As I’ve been working another blog post about my adventures in learning BDD ala MSpec , I ran into

Using Latest NUnit Version With MSpec « ThisGlobe.com wrote Using Latest NUnit Version With MSpec &laquo; ThisGlobe.com
on 11-11-2009 2:11 PM

Pingback from  Using Latest NUnit Version With MSpec « ThisGlobe.com

Marcin Obel wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 11-18-2009 2:59 AM

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

Marcin Obel wrote re: Step by Step to Using MSpec (Machine.Specifications) with ReSharper
on 11-23-2009 7:56 AM

Hi All,

I have released an intaller for MSpec 3.0. You can download it from my blog: marcinobel.com/.../mspec-bdd-installer

Please, let me know if everything works fine.

Elegant Code » Wrapping up the StructureMap Automocking Container wrote Elegant Code &raquo; Wrapping up the StructureMap Automocking Container
on 11-30-2009 2:21 AM

Pingback from  Elegant Code » Wrapping up the StructureMap Automocking Container

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems