background preloader

Rhino Mock

Facebook Twitter

TDD : Introduction to Rhino Mocks. Rhino Mocks is the most popular Mock Object Framework for .NET.

TDD : Introduction to Rhino Mocks

The purpose of this blog entry is to provide a brief introduction to Rhino Mocks. In particular, I want to describe how you can use Rhino Mocks when building ASP.NET MVC web applications. In the first part of this entry, I explain why Mock Object Frameworks are critical tools when performing Test-Driven Development. How to write better unit tests using RhinoMocks and stubs - Arrange Act Assert. When I was first learning about mocking frameworks (in my case RhinoMocks I found many examples on how to use them.

How to write better unit tests using RhinoMocks and stubs - Arrange Act Assert

One in particular (this post by Nikola Malovic) gave me the aha moment. This was written some time ago before .Net 3.5 was released. I wanted to show an example of a unit test before and after implementing a mocking framework. My aim is to show why and how mocking frameworks (including Moq) can help you write unit tests more productively without compromising code readability or maintainability.

Don’t worry if you’re new to writing unit tests or mocking frameworks, the examples should be easy enough to follow. So here’s my take on it. Anyway let’s get to the code. The emphasis shouldn’t be on the code per se, the aim is to keep it simple and easy to understand, after all validating objects is common practice! The code below shows: Customer objectA interface for the loggerA interface and class for customer validationA class for the customer service.

TDD - Rhino Mocks - Part 1 - Introduction. TDD – Rhino Mocks – Part 1 – Introduction Mocking is term which describes programing technique mostly used in Test Driven Development for writing tests of some entity with abstraction of its dependencies.

TDD - Rhino Mocks - Part 1 - Introduction

The best example is MVP design pattern where we are implementing presenter code by using Dependency Injection pattern to inject in presenter view implementation and maybe data source implementation during the run time. TDD - Rhino Mocks - Part 2 - Some common scenarios. TDD – Rhino Mocks – Part 2 – Some common scenarios In first part of the Rhino Mock explorations, I've tried to give answers on what is mocking, what are the different recording modes available in Rhino mocks, how to set up some of results etc.

TDD - Rhino Mocks - Part 2 - Some common scenarios

Today post would continue that by focusing on a few typical mocking tasks. To make this post easier to digest I've wrap those tasks into one "real world" example ShoppingCart is class defining the Checkout method which is been called in the moment user wants to make a payment for the purchased items in the cart. It has two properties: a) UserId - represents the id of the shopping cart user, b) CartAmount - represents the total amount to be charged to user for chosen cart items.Shopping cart checkout process loads user data using a type implementing IUserRepository. Although I prefer black box testing approach, we would take a peek at the shopping cart implementation because purpose of this post is not TDD test first methodology, but mocking examples.

Something to Read on the Train. This post is a general review of the Rhino.Mocks syntax.

Something to Read on the Train

While it is intended primarily as a quick reference for myself, I hope anyone who stumbles upon it can also find it useful. Rhino.Mocks is a .Net mocking framework which is extremely useful for stubbing out objects in your unit tests to ensure you’re only testing what you want to test and to allow you to control the environment/state/surroundings under which your tests execute. It is written by Ayende Rahien, the same guy that created nHibernate. Rhino Mocks 3.5. RhinoMocks 3.6.1. Install-Package RhinoMocks -Version 3.6.1 dotnet add package RhinoMocks --version 3.6.1 For projects that support PackageReference, copy this XML node into the project file to reference the package. paket add RhinoMocks --version 3.6.1 The NuGet Team does not provide support for this client.

RhinoMocks 3.6.1

Rhino.Mocks - the basics. Using.

Rhino.Mocks - the basics

How to use Rhino Mocks – documented through tests « Jon Kruger's Blog. Rhino Mocks : When to use Partial Mocks. I have been using Rhino Mocks for past couple of years as the Dynamic mocking framework.

Rhino Mocks : When to use Partial Mocks

It has support for different types of mocks like Strict mocks, Dynamic mocks and Partial mocks. The use of these types for specific use is bit confusing. Recently I had a wow moment when I found a real use of the Partial mock. This post is about that enlightenment. Most of the time I have relied on using Strict mock and stubs. How to use Partial Mocks Lets assume we are building a solution for calculating the phone bill for a hypothetical phone company. Public class PhoneBillCalculator private const int ROUNDED_DIGITS = 2 ; private const double CORPORATE_DISCOUNT_PERCENTAGE = 0.25 ; public PhoneBill GenerateBill ( Customer customer ) PhoneBill generateBill = new PhoneBill.

Rhino Mocks : Partially mock Internal method. In my previous post on Partial Mocks , I had made a note that the method which we intend to partially mock has to be a public virtual method.

Rhino Mocks : Partially mock Internal method

Immediately after publishing the post, I came across a post by Matt Roberts which demonstrated how to use InternalsVisibleTo attribute to expose protected methods to certain assemblies. In this post I’ll demonstrate how to use InternalsVisibleTo attribute to unit test internal methods. What’s the problem with earlier approach? If we look at the code from the previous post, we had to make the methods public to be able to mock them using Rhino Mocks. In my opinion, this violates the encapsulation as we are forced to expose methods which are not really supposed to be public. Apply InternalsVisibleTo attribute We add the following two lines to the AssemblyInfo.cs of the main project file. [ assembly : InternalsVisibleTo ( "PartialMocksExample.UnitTest" )] [ assembly : InternalsVisibleTo ( RhinoMocks .

Rhino Mocks Introduction - Ayende @ Wiki. Edit [I tried to create an example for this article, but the example was both contrived and didn't portray the possibilities correctly.

Rhino Mocks Introduction - Ayende @ Wiki

So most of the code samples here are taken from NHibernate Query Analyzer tests and are "real code".] The purpose of mock objects is to allow you to test the interactions between components, this is very useful when you test code that doesn't lend itself easily to state based testing. Most of the examples here are tests that check the save routine for a view to see if it is working as expected. The requirements for this method are: