

Var ingredients = _ingredientsProvider.GetIngredients() The cooked Pizza will be then returned.yum!īelow you can see the body of this method: This is basically a method that pretends to prepare a Pizza, getting ingredientsįrom somewhere and cooking it. The class is named PizzaMaker and offers a single method MakePizza() which will be tested. In the main project LiviosPizzeria you can find the main class, which we want to test. The first is the project under test and the second contains the unit tests related to it.

The solution attached contains two projects: one is LiviosPizzeria and one is LiviosPizzeria.Test.

I wrote a small application to use as a didactical tool.
#Rhino mocks code
The code used as an example should be very easy to understand. Introduction, I would strongly recommend to read the outstanding article about Mocks & Stubs from Martin Fowler and the Rhino Mocks documentation. (and more deeply) about the Mock World behind this short Need to know the basic concepts about mocking. To go through this article you have to be familiar with unit testing and you The one I'm using in this tutorial is Rhino Mocks, an open source framework relatively old, but still absolutely effective for most of the unit tests you can write nowadays with C# and. There are many frameworks that can be used to create mock objects when tests are written: some are commercials, some are free. In this article I'm also explaining the main differences between Mock Objects and Stub objects, often confused when discussing about tests.
#Rhino mocks how to
Overall, this just equaled to the entire AAA syntax in my level of pain reduction.This article is an easy introduction about how to use mock objects in unit tests and what are the main building blocks for an effective test that uses Mocks. Do(invocation => Assert.AreEqual(" foo", invocation.Arguments)) Īssert.AreEqual(" blah", stub.StringArgString(" foo")) Public void Can_inspect_method_arguments() You can also inspect the method arguments and make decisions based on that: Do(invocation => invocation.ReturnValue = " arg") Īssert.AreEqual(" arg", stub.StringArgString(" ")) Oh, and it even allows you to directly modify the method return value: Stub.Stub(x => x.StringArgString(Arg.Is(" ")))Īssert.AreEqual(" blah", stub.StringArgString(" ")) Var stub = MockRepository.GenerateStub() Public void Can_use_when_called_to_exceute_code_when_exceptation_is_matched_ \ And once you let go of that, you can get this really cool syntax: There isn't any really reason why this should be so. Once I realized that, I realized that my issue all along was the insistence that it should be strongly typed. But most often, you don't care about that. Here is the deal, the original design of Do() include matching the called method signature. I should be trying to get the user to do the work, instead of the compiler.

I was writing something today and I really want to be able to use Do(), but I had zero intention of getting into that mess again. The fault was with the annoying compiler, and like many other language limitations, you just have to live with it. As a result, they required you to explicitly pass a delegate type, where a pain to use and major eye sore.įor a long time, I accepted that there is nothing to do. I designed them at the 1.1 days, when we didn't have such things as anonymous delegates or lambda. One of the major pain points for me in Rhino Mocks has always been the Callback() and Do() delegates.
