Skip to content
March 24, 2008 / kiranpatils

ExpectationViolationException+Rhino Mocks

Why ExpectationViolationException?

Error:

HelloRhino.MyRhinoImplementationTest.HelloRhinoMock_Test : Rhino.Mocks.Exceptions.ExpectationViolationException : IMyRhinoImplementation.HelloRhinoMocks(“ABC”); Expected #1, Actual #0.

Rhino Mock supports following three types of Mock Objects:
    * Mock Objects – Strict replay semantics. – Created by calling CreateMock(): WE ARE GOING TO USE THIS ONE
    * Dynamic Mock – Loose replay semantics. – Created by calling DynamicMock()
    * Partial Mock – Mock only requested methods. – Created by calling PartialMock()

Strict replay semantics: only the methods that were explicitly recorded are accepted as valid. This mean that any call that is not expected would cause an exception and fail the test. All the expected methods must be called if the object is to pass verification.

Loose replay semantics: any method call during the replay state is accepted and if there is no special handling setup for this method a null or zero is returned. All the expected methods must be called if the object is to pass verification.

Mocking only requested methods: this is available for classes only. It basically means that any non abstract method call will use the original method (no mocking) instead of relying on Rhino Mocks’ expectations. You can selectively decide which methods should be mocked.

Mock object code
[TestFixture]
    public class MyRhinoImplementationTest
    {
        MyRhinoImplementation myimple;
        IMyRhinoImplementation mockMyRhinoImplementation;
        MockRepository mockrepository;
        [SetUp]
        public void DoSetup()
        {
            //Mock
            myimple = new MyRhinoImplementation();
            mockrepository = new MockRepository();
            mockMyRhinoImplementation = mockrepository.CreateMock<IMyRhinoImplementation>();
            RhinoMocks.Logger = new TextWriterExpectationLogger(Console.Out);
           
        }
        [TearDown]
        public void DoCleanup()
        {
            myimple = null;
            mockrepository = null;
            mockMyRhinoImplementation = null;           
        }

        [Test]
        public void HelloRhinoMock_Test()
        {
            //Record our Expectations
            using (mockrepository.Record())
            {
                                Expect.Call(mockMyRhinoImplementation.HelloRhinoMocks(“ABC”)).Return(“Hi!! from Rhino Mocks to:ABC”);
            }           
            // Tell Rhino Mocks We’re Done Setting Expectations
            mockrepository.ReplayAll(); // it replay all mocks from reopository

            //We played a Expectation now lets check that our Recorded song is playing as per
            //our expectation or not? 
  //THIS LINE WILL THROW AN EXCEPTION BECAUSE WE Must needs to call Method which we have expcted to call for this
  //Example:mockMyRhinoImplementation.HelloRhinoMocks(“ABC”)          
  mockrepository.VerifyAll(); //Verifies all the mocks from the repository

            //Aboves VerifyAll block throws an Error
            //HelloRhino.MyRhinoImplementationTest.HelloRhinoMock_Test : Rhino.Mocks.Exceptions.ExpectationViolationException :
            //IMyRhinoImplementation.HelloRhinoMocks(“ABC”); Expected #1, Actual #0.
           
            //This exception is thrown when there is a an expectation violation.
        }
    }

Mock objects are useful when you want complete control over what happens to the mocked object.

Solution:
in HelloRhinoMock_Test() Method do the changes:
    //-*****
            mockMyRhinoImplementation.HelloRhinoMocks(“ABC”); //Line added–For solving ExepcationviolationException
            //-*****
            mockrepository.VerifyAll(); //Verifies all the mocks from the repository

Conclusion:
So for using Rhino Mocks just follow the steps as under with Mock Object:
1.Create mock by: mockrepository.CreateMock<IMyRhinoImplementation>();
2.Record your expactations: in Record Block.
3.call ReplayAll to tell rhino Mocks that you are done with recording
4.MOST IMP. STEP:Call expected method here for example: mockMyRhinoImplementation.HelloRhinoMocks(“ABC”)
 NOTE: Must need to pass the same arguments what you have recorded else it will again throw ExpectationViolationException.
5.Call VerifyAll();          

Reference:
http://www.ayende.com/Wiki/(S(wp4edvbnsrsr2reccscyaq2w))/Default.aspx?Page=Rhino+Mocks+Introduction

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: