Introducing Machine.Specifications.Mvc
Tags: BDD, MSpec, MVC, TDD, Testing January 18th, 2010Hot on the heels of my Machine.Specifications.AutoMocking framework, I am please to announce another extension to the awesome Machine.Specifications BDD testing framework.
Machine.Specifications.Mvc is a set of extensions for testing ASP.Net MVC specific types.
It aims to ease the testing of ActionResult objects returned from MVC Controllers by providing an MSpec BDD syntax over these types.
Here’s a list of its current features:
ActionResult extensions
ShouldBeAView()
e.g.
result.ShouldBeAView();
ShouldBeARedirectToRoute()
e.g.
result.ShouldBeARedirectToRoute();
ShouldBeARedirect()
e.g.
result.ShouldBeARedirect();
Each expose the And() chaining command, returning the strongly typed ActionResult.
e.g.
result.ShouldBeAView().And().ViewName.ShouldEqual(“NotFound”);
result.ShouldBeARedirect().And().Url.ShouldEqual(“http://someurl.com“);
result.ShouldBeARedirectToRoute().And().RouteName.ShouldEqual(“RouteName”);
ViewResult specific extensions
ShouldUseDefaultView()
e.g.
result.ShouldBeAView().And().ShouldUseDefaultView();
ShouldHaveModelOfType<T>()
e.g.
result.ShouldBeAView().And().ShouldHaveModelOfType<Person>();
ShouldHaveModelOfType<T>() also exposes the And() chaining command, returning the model strongly typed as T
e.g.
result.ShouldBeAView().And().ShouldHaveModelOfType<Person>().And().Id.ShouldEqual(1);
RedirectToRoute specific extensions
ControllerName()
e.g.
result.ShouldBeARedirectToRoute().And().ControllerName().ShouldEqual(“Person”);
ActionName()
e.g.
result.ShouldBeARedirectToRoute().And().ActionName().ShouldEqual(“List”);
Further ActionResult extensions
Model<T>()
Provides a shortcut straight to the ViewData.Model property of an ActionResult cast as a ViewResult
e.g.
result.Model<Person>().Name.ShouldEqual(“James Broome”);
ShouldRedirectToAction<TController>(Expression<Action<TController>> action)
Provides a strongly typed shortcut to check an ActionResult cast as a RedirectToRoute against Actions on Controllers
e.g.
result.ShouldRedirectToAction<HomeController>(x => x.Index());
Where can I get it?
Source code is available on github at http://github.com/jamesbroome/Machine.Specifications.Mvc/

January 25th, 2010 at 6:10 am
[...] to VoteIntroducing Machine.Specifications.Mvc (1/18/2010)Monday, January 18, 2010 from JamesHot on the heels of my Machine.Specifications.AutoMocking [...]
April 13th, 2010 at 11:20 pm
[...] Introducing Machine.Specifications.Mvc [...]
April 20th, 2011 at 7:24 pm
It seems that this extension is not compatible with current version of MSpec. For example, ShouldBeAView failed when it should be succeed.
Anyway it’s a good library that I’ve already learned a lot from. I am thankful for that.
July 22nd, 2011 at 3:35 pm
Would be cool if you could get it on Nuget. Cheers.