views:

504

answers:

2

Hi,

I've been looking around for some decent information on using Rhino Mocks 3.5+ with the AAA syntax. I find a lot of blogs that have a mix of things from the old and new which seem to make it more difficult to figure out how to use it. Would would be great would be if there were a Rhino Mocks AAA Cheat Sheet like was done for an earlier version. Is it required that you know everything about the older versions of Rhino to actually use the newer version? I'm sure if I were an expert that I would love all the capabilities in Rhino, but for now I'm just swimming in information. Any pointers or good links would be totally appreciated!

thanks in advance! Bill

+1  A: 

I assume that you're familiar with official documentation, which is quite good in my opinion. My suggestion is to try to use Rhino, and when you encounter some more specific problem search for solution wither on SO or somewhere else. I don't think that there is a comprehensive cheat sheet for Rhino mocks. I guess you'll have more luck asking "How can I do this and that using Rhino Mocks"

Edit: Well, you don't need to use Record/Playback when targeting AAA. There are three steps involved in AAA:

  • Arrange, which stands for setting up the behavior of mocked class, for example

.

IOmicronDll mockWrapper = MockRepository.GenerateMock<IOmicronDll>();
mockWrapper.Expect(wrapper => wrapper.Lock(1, ref errors)).OutRef(string.Empty).Return(true).Repeat.Any();
mockWrapper.Expect(wrapper => wrapper.Exec(1, "sys:cfg?(type)", ref output, ref errors)).OutRef("1,CMC 56,0;", "").Return(true).Repeat.Any();
mockWrapper.Expect(wrapper => wrapper.Exec("1", "sys:cfg?(type)", ref output, ref errors)).OutRef("1,CMC 56,0;", "").Return(true).Repeat.Any();
Microsoft.Practices.Unity.UnityContainer c = new Microsoft.Practices.Unity.UnityContainer();
c.RegisterInstance<IOmicronDll>(mockWrapper);
  • Act, which stands for executing the tests

    public Omicron(int deviceID)
    {
        try
        {
            if (g_Omicron == null)
                g_Omicron = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<CMEngineWrapper.IOmicronDll>();
            m_UniqueIdentifier = Guid.NewGuid();
            m_Logger = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<AdvAdmittance.Framework.ILogger>();
            m_ID = deviceID;
            GetConfiguration();
            g_InstancesCount++;
            m_PollThread = new Thread(new ThreadStart(DoPoll));
            m_PollThread.Start();
        }
    
  • And Assert, which stands for veryfing the results

.

Assert.AreEqual("CMC 56", omicron.Type);
mockWrapper.AssertWasCalled(wrapper => wrapper.Release(), options => options.Repeat.AtLeastOnce());

Perhaps the above examples aren't the best, but might get you into right direction.

Bolek Tekielski
Thanks and yes - Ayende has some great stuff for sure. Just as a newbie wanting to learn how to do AAA with Rhino it almost seems that I need to do it using Record/Playback and then as specific questions how to do it AAA style? Course, I'd have to learn the Record/Playback first. That's why I'm confused where to start to even ask a good question.
Bill Campbell
My suggestion is to skip Record/Playback altogether. I think the AAA syntax is cleaner and easier to understand and you don't really need to know how it was done in the past using R/P. Start by writing some simple unit tests and make assertions the way you think Rhino works. This will then help you get some practice with AAA syntax.
Igor Brejc
-1 the idea of AAA is that you set up stubs with behavior at the start, and make assertions at the end (as shown in the documentation you link to). But you are using mocks instead of stubs, you set expectations at the start, and the expectations are never even verified because you don't use `VerifyAllExpectations` anywhere. It is evident from this follow-up question from Bill that you confused him: http://stackoverflow.com/questions/2349454/rhinomocks-aaa-syntax/2350864#2350864
Wim Coenen
+1  A: 

First make sure you know what you mean for each A in AAA. You may know but I'll include my working definitions for completeness of the answer:

  • Arrange is where I set up inputs, mocks/stubs, the object with the method under test
  • Act is where I call the method under test
  • Assert is where I verify things occurred or didn't according to expectation

I like to put comments in my test code to remind me to think about each of those things. An example may help clarify: Suppose I have a service layer class which uses two provider layer classes, one from an "old" system and one from a "new" system; I am testing that the method which copies old things to the new system calls the "CreateThing" method one time for each old thing found.

[Test]
public void Should_create_new_Thing_for_each_old_Thing()
{
  // -----
  // arrange

  // hardcode results from old system provider
  List<Thing> oldThings = new List<Thing> { ... };

  // old system provider
  var oldProvider = MockRepository.GenerateStub<IOldSystemProvider>();
  oldProvider.Stub(m=>m.GetThings()).Return(oldThings);

  // new system provider
  var newProvider = MockRepository.GenerateStub<INewSystemProvider>();

  // service object
  var svc = new MyService(oldProvider, newProvider);

  //-----------
  // act
  var result = svc.CopyThings();

  //------------
  // assert
  oldThings.ForEach(thing => 
                    newProvider.AssertWasCalled(prov => prov.CreateThing(thing)));
}
JohnKeller