background preloader

Mocks Aren't Stubs

Mocks Aren't Stubs
The term 'Mock Objects' has become a popular one to describe special case objects that mimic real objects for testing. Most language environments now have frameworks that make it easy to create mock objects. What's often not realized, however, is that mock objects are but one form of special case test object, one that enables a different style of testing. In this article I'll explain how mock objects work, how they encourage testing based on behavior verification, and how the community around them uses them to develop a different style of testing. I first came across the term "mock object" a few years ago in the Extreme Programming (XP) community. But as often as not I see mock objects described poorly. This difference is actually two separate differences. Regular Tests I'll begin by illustrating the two styles with a simple example. These two behaviors imply a couple of tests, these look like pretty conventional JUnit tests. Tests with Mock Objects Using EasyMock class OrderStateTester... Related:  AgileTesting Frameworks

TDD : Introduction to Moq In this post, I provide an introduction to Moq which is the newest of the Mock Object Frameworks. Moq is promoted by its creators as easier to learn and use than other Mock Object Frameworks such as Rhino Mocks and TypeMock Isolator. Moq takes advantage of recent VB.NET and C# language features such as lambdas and generics. When creating mock objects with Moq, you use lambda expressions to represent the methods and properties that you want to mock. According to the authors of Moq, you can pronounce “Moq” either like “Mock-You” or like “Mock”. Moq is actively maintained by Daniel Cazzulino. This post is about Moq version 2.5. Some Background, Some Philosophy, and Some Controversy There is just something about Mock frameworks that generate controversy. Some people have criticized Typemock Isolator for being too powerful. In this paper, Fowler makes several distinctions.

CookBook - googlemock - Google C++ Mocking Framework Cookbook - Google C++ Mocking Framework You can find recipes for using Google Mock here. If you haven't yet, please read the ForDummies document first to make sure you understand the basics. Note: Google Mock lives in the testing name space. Mocking Private or Protected Methods You must always put a mock method definition (MOCK_METHOD*) in a public: section of the mock class, regardless of the method being mocked being public, protected, or private in the base class. class Foo { public: ... virtual bool Transform(Gadget* g) = 0; protected: virtual void Resume(); private: virtual int GetTimeOut();}; class MockFoo : public Foo { public: ... // The following must be in the public section, even though the // methods are protected or private in the base class. Mocking Overloaded Methods You can mock overloaded functions as usual. class Foo { ... // Must be virtual as we'll inherit from Foo. virtual ~Foo(); // Overloaded on the types and/or numbers of arguments. virtual int Add(Element x); virtual int Add(int times, Element x); Some tips:

Rhino Mocks Examples, with a fix | Girl Writes Code Jon Kruger created an excellent explanation of Rhino Mocks, using unit tests to demonstrate and illuminate the syntax and capabilities. (Found via @gar3t .) It needs one small correction, which I’d like to write about here so that I can link to and support Jon’s work, and because it gives the opportunity to clarify a subtle distinction between mocks and stubs and verified expectations. First, go check out Jon’s code , then come back here. The problem lies in the following test. [ Test ] public void Another_way_to_verify_expectations_instead_of_AssertWasCalled() var stub = MockRepository .GenerateStub< ISampleClass >(); // Here I'm setting up an expectation that a method will be called stub.Expect(s => s.MethodThatReturnsInteger( "foo" )).Return(5); //Sneaky Sharon comments out the "Act" part of the test: //var output = stub.MethodThatReturnsInteger("foo"); //Assert.AreEqual(5, output); // ... and now I'm verifying that the method was called stub.VerifyAllExpectations(); public class MyRealClass

Introducing BDD « History: This article first appeared in Better Software magazine in March 2006. It has been translated into Japanese by Yukei Wachi, Korean by HongJoo Lee, Italian by Arialdo Martini and French by Philippe Poumaroux and most recently into Spanish by Oscar Zárate, Turkish by Selim Öber and Russian. I had a problem. While using and teaching agile practices like test-driven development (TDD) on projects in different environments, I kept coming across the same confusion and misunderstandings. The deeper I got into TDD, the more I felt that my own journey had been less of a wax-on, wax-off process of gradual mastery than a series of blind alleys. My response is behaviour-driven development (BDD). Test method names should be sentences My first “Aha!” renders something like this: The word “test” is stripped from both the class name and the method names, and the camel-case method name is converted into regular text. A simple sentence template keeps test methods focused I had introduced a bug.

V1_7_Primer - googletest - Getting started with Google C++ Testing Framework - Google C++ Testing Framework Google C++ Testing Framework helps you write better C++ tests. No matter whether you work on Linux, Windows, or a Mac, if you write C++ code, Google Test can help you. So what makes a good test, and how does Google C++ Testing Framework fit in? We believe: Tests should be independent and repeatable. Since Google C++ Testing Framework is based on the popular xUnit architecture, you'll feel right at home if you've used JUnit or PyUnit before. Note: We sometimes refer to Google C++ Testing Framework informally as Google Test. To write a test program using Google Test, you need to compile Google Test into a library and link your test with it. Once you are able to compile the Google Test library, you should create a project or build target for your test program. If you still have questions, take a look at how Google Test's own tests are built and use them as examples. When using Google Test, you start by writing assertions, which are statements that check whether a condition is true. namespace {

Mocking Property Getters and Setters In typical unit testing scenarios , mocking property getters and setters isn't high on the list of areas of concern. Where it usually comes into play is when an entity’s properties’ values (or the act of setting them) are part of the scaffolding, or behavior for the system under test. An example is a scenario where entities are publishers of events and the system under test is the subscriber. Listing 1 – INotifyPropertyChanged Interface The premise of the pattern is that any time a property changes on the model, the PropertyChanged event gets raised, notifying any subscribers of the change. By implementing this interface on the models in your application (in addition to the benefits that you get from the XAML databinding engine), you can build an “IsDirty” flag that gets set if any property on your model changes. For our example, we are going to create an IEntity interface as well as a EntityBase class that implements the IEntity interface. Mocking Property Getters Summary Happy Coding!

Adapting to Inversion of Control and Dependency Injection You might have come across the phrases IoC, Dependency Injection, Mocking among others, these are commonly used when talking about “Inversion Of Control” which is the full meaning of the abbreviation IoC. So what is this “IoC” that everyone is talking about? Inversion of control is a principle in Software Engineering, let’s just take a look at the Wikipedia definition of this In practice, Inversion of Control is a style of software construction where reusable generic code controls the execution of problem-specific code. Rather than explaining the text below, let me show an example of an application that does not follow this principle, but will when we’re done here! This solution simulates an order process, where we’ve got a window where we can place some kind of order and then we’ve got some sort of payment provider that executes the order request. The order form is very simple and looks like this: When we press the button this is what’s going to happen: Take a look at this line of code:

List of unit testing frameworks This page is a list of tables of code-driven unit testing frameworks for various programming languages. Some but not all of these are based on xUnit. Columns (Classification)[edit] Name: This column contains the name of the framework and will usually link to it.xUnit: This column indicates whether a framework should be considered of xUnit type.TAP: This column indicates whether a framework can emit TAP output for TAP-compliant testing harnesses.Generators: Indicates whether a framework supports data generators. Languages[edit] ABAP[edit] ActionScript / Adobe Flex[edit] Ada[edit] AppleScript[edit] ASCET[edit] ASP[edit] BPEL[edit] C[edit] C#[edit] See .NET programming languages below. C++[edit] Cg[edit] CFML (ColdFusion)[edit] Clojure[edit] Cobol[edit] Common Lisp[edit] Curl[edit] Delphi[edit] Emacs Lisp[edit] Erlang[edit] Fortran[edit] F#[edit] Groovy[edit] All entries under Java may also be used in Groovy. Genexus[edit] Haskell[edit] Haxe[edit] HLSL[edit] ITT IDL[edit] Internet[edit] Java[edit] JavaScript[edit]

Setting PropertyBehavior On All Properties With Rhino Mocks Although I am a big fan of Rhino Mocks , I typically favor State-Based over Interaction-Based unit testing , though I am not totally against Interaction Based testing. I often use Rhino Mocks to dynamically create Dummy objects and Fake objects rather than true Mocks, based on this definition given by Martin Fowler . Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists. Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example). Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test. Fortunately Rhino Mocks is well suited to this purpose. Here we have a very simple interface. public interface IAnimal { int Legs { get; set; } } Next, we have a simple class we want to test that interacts with IAnimal instances. Fair enough.

Balsamiq aharin/inproctester C# Corner: Inside Mocking Frameworks C# Corner Use Mocking Frameworks to Improve Code Quality In today's agile/TDD world, mocking has become an important tool in the development of a good, testable design. It's important to understand your mocking framework to make sure you can maximize its benefit. But before I talk about mocking frameworks, I want to review a little bit about subclassing in .NET. Even if you're familiar with subclassing, inheritance and polymorphism, I encourage you to read on. As I'm sure many of you know, to override a method is to create a subclass of an object and change the behavior of a method. public class SimpleInterestCalculator { public double Principal { get; set; } public double InterestRate { get; set; } public double Time { get; set; } public virtual double CalculateInterestAmount() { return Principal*InterestRate*Time; } } Note that we've made CalculateInterestAmount() virtual. Of course, you don't always have to change the behavior. And here's how we can use this to test the "Bank" class: