Rhino Mocks Training Content
I tried searching on this for a personal assignment but couldn’t locate one. So decided to create it myself. Hope you find it useful. Let me know if your UG needs a session on this
.
RHINO MOCKS
1 Day Session
Software Requirements: VS.NET 2008 with SP1, Rhino Mocks 3.5, SQL SERVER 2005 / 2008, Windows XP / Vista
Design Principles
Dependency Injection
Aspect Oriented Programming
TDD Development
Mocks
Introduction to Mocks
Unit Testing with Mocks
Types of Mocks
Dummy
Fake
Stubs
Mocks
State Verification vs. Behavior Verification
TDD vs. BDD
Rhino Mocks
Introduction to Rhino Mocks
Record / Replay model
Mock Types
Strict Mocks
Dynamic Mocks
Partial Mocks
Expectations and Setup
Methods
Out Ref Parameters
Events
Recursive Mocking
Ordered / Unordered Expectations
Call Repetition
Argument Constraints
Demos
Unit Testing Links
I have been using Unit Testing for quite some time now. So I thought to collect some good links on it, for anybody who is about to embark this journey.
One such link is this, which does a nice differentiation between unit testing, integration testing & system testing.
Another awesome article is here and a small follow up on it.
I am not sure if I should include this one, here, but i will do it just to indicate the importance of unit testing.
This one is an interesting way of naming unit tests.
I would appreciate if you can append the ones which have helped you.
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
{
[TestMethod]
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);
}
[TestMethod]
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 =
mockRepository.Stub<IProductRepository>();
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
{
SetupResult
.For(productRepository.GetProducts())
.Return(_fakeProducts);
}
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
.