Don’t Moq Me I’m Becoming Fluent

By Mike Hanson at April 14, 2011 17:14
Filed Under: .NET, Mocks, MS Test, Testing

Having recently finished a contract I am “resting” for a while and got to spend some time on my personal projects.  While doing so I took a closer look at NuGet and while browsing the gallery I cam across two packages that piqued my interest.  NSubstitute and Fluent Assertions.

 

Previously I have been a big fan of Moq and recommended it in my professional life as well as for my personal projects.  After visiting the home of NSubstitute and reading the getting started guide at http://nsubstitute.github.com/help/getting-started/ I decided to see what the world was thinking and after a bit of Googling came across http://www.richard-banks.org/2010/07/mocking-comparison-part-1-basics.html, this is a great series comparing Rhino Mocks Moq and NSubstitute.  It was enough for me so no more Moq I have switched to NSubstitute and if you like concise highly readable test code you should take a look.

 

I also took a look at http://fluentassertions.codeplex.com/ to see what Fluent Assertions was all about and was impressed.  I know it is not good form to assert too many things in a test, but sometimes it just makes sense and writing multiple Assert.* statements never looks pretty, especially when you are repeatedly passing the same object to the assertion method.  Fluent Assertions provides a mass of Extension Methods that IMHO significantly improves the readability of test code.  A side effect of using Fluent Assertions is that your test code becomes highly portable, being able to switch from one testing framework to another very easily (Not that I have had to to do this frequently, but it is a nice thought)

 

I am not going to regurgitate all the documentation since the links above provide all you need but I’m providing an extract from a test for a presentation model I am working that I hope demonstrates how much cleaner and readable the code becomes using NSubstitute and Fluent Assertions.

 

   1: [TestInitialize]
   2: public void Initialise()
   3: {
   4:     this.controller = Substitute.For<IController>();
   5:  
   6:     this.activityLookupServiceClient = Substitute.For<IActivityLookupServiceClient>();
   7:     this.activityLookupServiceClient.GetActivityTypes().Returns(activityTypes.ToObservable());
   8:  
   9:     this.activityTemplateServiceClient = Substitute.For<IActivityTemplateServiceClient>();
  10:     this.activityTemplateServiceClient.GetActivityTemplates().Returns(activityTemplates.ToObservable());
  11: }
  12:  
  13: [TestMethod]
  14: public void OnCreationctivityTypesContains2ListItemModels()
  15: {
  16:     var model = new NewActivityModel(this.controller, this.activityLookupServiceClient, this.activityTemplateServiceClient);
  17:     model.ActivityTypes
  18:         .Should()
  19:         .NotBeEmpty()
  20:         .And.HaveCount(2)
  21:         .And.ContainItemsAssignableTo<IListItemModel>();
  22: }
  23:  
  24: [TestMethod]
  25: public void OnCreationFirstActivityTypeIsSetAsSelected()
  26: {
  27:     var model = new NewActivityModel(this.controller, this.activityLookupServiceClient, this.activityTemplateServiceClient);
  28:     model.SelectedActivityType
  29:         .ShouldHave()
  30:         .Properties(d => d.Id, d => d.Name)
  31:         .EqualTo(activityTypes[0]);
  32: }
  33:  
  34: [TestMethod]
  35: public void OnCreationSessionTypesContains1ListItemWithIdOfSessionTypeThatIsChildOfFirstActivityType()
  36: {
  37:     var model = new NewActivityModel(this.controller, this.activityLookupServiceClient, this.activityTemplateServiceClient);
  38:     model.SessionTypes
  39:         .Should()
  40:         .NotBeEmpty()
  41:         .And.HaveCount(1)
  42:         .And.ContainItemsAssignableTo<IListItemModel>();
  43:     model.SessionTypes
  44:         .First()
  45:         .ShouldHave()
  46:         .Properties(d => d.Id, d => d.Name)
  47:         .EqualTo(activityTypes[0].SessionTypes[0]);
  48: }
blog comments powered by Disqus

Tag cloud

Previous Articles