Monday, April 14, 2014

an example of a test using Rhino Mocks

using Whatever;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using StructureMap;
namespace DALTests.Controllers
{
   [TestClass]
   public class ThingamabobTests
   {
      [TestInitialize()]
      public void Setup()
      {
         ObjectFactory.Initialize(x => x.AddRegistry(new MyRegistry()));
      }
      
      [TestMethod]
      public void Test()
      {
         
//Arrange
         var fooProvider = ObjectFactory.GetInstance<FooProvider>();
         var barProvider = ObjectFactory.GetInstance<BarProvider>();
         var bazProvider = ObjectFactory.GetInstance<BazProvider>();
         
         fooProvider.Expect(x => x.fooState).Return(null);
         barProvider.Expect(x => x.barStuff()).Return(new List<Qux>());
         
         
//Act
         var thingamabob = new Thingamabob(fooProvider, barProvider, bazProvider);
         thingamabob.Act();
         
         
//Assert
         fooProvider.VerifyAllExpectations();
         barProvider.VerifyAllExpectations();
      }
   }
}

 
 

There is also a way to sanity check the number of times a method is called with syntax such as:

  • barProvider.Expect(x => x.barStuff()).Repeat.Never();
  • barProvider.Expect(x => x.barStuff()).Repeat.Once();
  • barProvider.Expect(x => x.barStuff()).Repeat.Twice();
  • barProvider.Expect(x => x.barStuff()).Repeat.Times(13);

No comments:

Post a Comment