Rhino.Mocks ExpectationViolationException

I'm trying out Rhino.Mocks in my NUnit test suites. Why Rhino.Mocks instead of NMock, you ask? Well, because I've heard that Rhino.Mocks can mock classes as well as interfaces.

Anyway, I was running into a little bit of a rookie's setback. I set up my mock objects like this:


1 const string TESTNAME = @"My Component Name";
2 MockRepository mocks = new MockRepository();
3 MyLib.MyTestClass tw = mocks.CreateMock<MyLib.MyTestClass>();
4 Expect.Call(tw.get_Name()).Return(TESTNAME);
5 mocks.ReplayAll();
// Test first invocation of get_Name()
6 Assert.AreEqual(tw.get_Name(), TESTNAME);
// Test second invocation of get_Name()
7 Assert.AreEqual(tw.get_Name(), TESTNAME);



I should probably add that MyLib.MyTestClass is a COM Interop class.

Executing line 7 resulted in this exception: Rhino.Mocks.Exceptions.ExpectationViolationException : _MyTestClass.get_Name(); Expected #1, Actual #2.

A little bit of googling did not immediately hint at the proper solution for me, but looking at the API documentation turned up this method: Repeat.AtLeastOnce(). So adjusting line 4 above to this did the trick:


4 Expect.Call(tw.get_Name()).Return(TESTNAME).Repeat.AtLeastOnce();

0 comments: