Mocking a void function with Rhino Mocks

In this small example we will use Rhino Mocks to mock a void function by either using a delegate or by using lambda when using the Expect.Call method.

Suppose we had this Interface and we wanted to mock it and set it up to expect the Update method to be called.


    public interface IUserDao
    {
        void Add(User user);
        void Update(User user);
        void Remove(User user);
    }

Here is the delegate approach


            MockRepository mocks = new MockRepository();
            IUserDao userDao = mocks.DynamicMock< IUserDao >();

            Expect.Call(delegate { userDao.Update(_user); });

Here is the lamda approach


            MockRepository mocks = new MockRepository();
            IUserDao userDao = mocks.DynamicMock< IUserDao >();

            Expect.Call(() => userDao.Update(_user));

Here is the complete NUnit and Rhino Mock test


        [Test]
        public void UpdateUser_CorrectInfo_UpdateCalled()
        {
            //Arrange
            MockRepository mocks = new MockRepository();
            IUserDao userDao = mocks.DynamicMock< IUserDao >();
            Expect.Call(() => userDao.Update(_user));
            mocks.ReplayAll();

            IUserService userService = new UserService(userDao);

            //Act
            userService.Update(_user);

            //Assert
            mocks.VerifyAll();
        }



Comments

  1. Ajay September 17th

    Comment Arrow

    Great. It’s help me alot.


Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.