Niraj Bhatt – Architect's Blog

Ruminations on .NET, Architecture & Design

Rhino Mocks Links

I am no mockist. But I had to upgrade myself on this for one of my clients. Below are the few links I found useful. Hope they will help you as well. You can download Rhino Mocks here.
If I were to give you only one link, I would give you this one by Stephen Walther. Most accurate, to the point definition of mock types (strict, dynamic & partial) can be found here. If you prefer to get deeper or want an immediate answer to your question you can try out official forum.
Of course it all starts here at creator’s (Ayende Rahien’s) offical site. A good writeup by creator (but slightly old I guess)  can be found here. Quick Reference can be found here.
Couple of other articles can be found here – Part I & Part II though I haven’t read them completely.

Classic article by martin fowler MocksVsStubs is an essential read as well one on Dependency Injection which is critical in order to leverage on Rhino Mocks. The code sample below by me is an example of state based vertification using Stubs. To see a behavior based verification test in action using mocks you can refer this example.

I have slightly altered the code of Stephen Walter to take it a bit closer to real world (Though I only hope so🙂 ). Suppose you have 2 interfaces IProduct & IProductRepository

public interface IProduct
        string Name { get; set; }
        int Price { get; set; }

public interface IProductRepository
        List<IProduct> GetProducts(); // Get the products from database

You don’t have the concrete implementations of either & you have been asked to implement a class called ProductManager which decides on whether a product is eligible for discounts. It also sums up the total bill for a group of products passed to it. Using Dependency injection the code for ProductManager class would like below (you can ofcourse follow a TDD approach by writing Unit Test first but I am just trying to simplify things here).

public class ProductManager
        public int GetTotalPrice(IProductRepository productRepository) // Dependency being injected rather than instantiation
            int total = 0;
            List<IProduct> products = productRepository.GetProducts();
            foreach (var product in products)
                total += GetDiscountedPrice(product); // Get discounted price as below
            return total;

        public int GetDiscountedPrice(IProduct product) // Ditto
            if (product.Price > 1000)
                return product.Price – 50; // If Price is more than 1000 give a discount of 50
            return product.Price;

Now let’s say you want to test the above code. For that you require concrete implementations of interfaces & setup the corresponding infrastructure as required. But those implementations are currently being worked on you don’t have time to wait for them to be available. You want to move ahead quickly with unit test related to your code. Here comes Rhino Mocks to your rescue.

[TestClass] // VSTS attributes, you can use NUnit as well
public class ProductManagerTests
        public void GetDiscountedPriceTest()
            ProductManager manager = new ProductManager();

            IProduct product = MockRepository.GenerateStub<IProduct>(); // Create a stub
            product.Name = “TV”;
            product.Price = 12000;

            int discountedPrice = manager.GetDiscountedPrice(product); // Passing stub as a parameter
            Assert.AreEqual(11950, discountedPrice);

        public void GetTotalPriceTest()
            ProductManager manager = new ProductManager();

            MockRepository mockRepository = new MockRepository(); // Create the repository as we need for the methods like record, playback etc.

            IProduct _p1 = mockRepository.Stub<IProduct>(); // Another way to create stub. N.B. you can replace GetDiscountedPriceTest with this syntax although formal is more crisp.
            IProduct _p2 = mockRepository.Stub<IProduct>();
            IProduct _p3 = mockRepository.Stub<IProduct>();

            _p1.Name = “A”;
            _p1.Price = 1050;
            _p2.Name = “B”;
            _p2.Price = 1050;
            _p3.Name = “C”;
            _p3.Price = 1050;

            List<IProduct> _fakeProducts =
                new List<IProduct>() {_p1, _p2, _p3}; // Your fake products, without mocks you would have to get them from a persistent store like DB.

            IProductRepository productRepository =

            using(mockRepository.Record()) // Record the call, and if the call is made later to the recorded method the result returned will the one you have setup

            int totalPrice = manager.GetTotalPrice(productRepository);
            Assert.AreEqual(3000, totalPrice);

For a  more thorough understanding refer to links at the starting of the article. Happy mocking🙂 .

One response to “Rhino Mocks Links

  1. Nitin Nayyar June 27, 2012 at 10:30 am

    Good One.Thanks

Leave a Reply

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

You are commenting using your 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: