background preloader

Moq - The simplest mocking library for .NET and Silverlight

Moq - The simplest mocking library for .NET and Silverlight
Related:  Agile

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. The emphasis on lambdas enables Moq to provide a very clean syntax for representing expectations, parameter constraints, and return values. 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. Installing and Setting Up Moq

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. Since then I've run into mock objects more and more. 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.

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. Programmers wanted to know where to start, what to test and what not to test, how much to test in one go, what to call their tests, and how to understand why a test fails. 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: I had introduced a bug. and one for the event:

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. It carries the strong connotation that the reusable code and the problem-specific code are developed independently, which often results in a single integrated application. 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! The order form is very simple and looks like this: When we press the button this is what’s going to happen: MessageBox.Show(result.ToString());} So far so good!

Balsamiq Getting started with a Mocking Framework In previous articles we’ve looked at how to increase code quality by either introducing test drive development for new features or using inversion of control. There are of course many other aspects that come into play when you want to reach high-quality code and this article is going to introduce you to mocking that will help you along the way. You might have come across the word “mock” before, when for instance a UI designer for a Windows Phone application on your team puts together a picture of how the application might look when it is finished: Where do the buttons go? What text might be displayed when the button is pressed? The word “mock” means to fake something, which is exactly what we want to do here; we want to fake the layout to give the customer an idea of how the application will look or how it will behave. How does this apply to programming? This class has only one purpose and it is to withdraw money from the customers account. 1.public interface IPaymentProvider 06. 03. if (!

Scrum Process for Software Development using Microsoft Visual Studio Scrum 1.0 Process Template Picture 1: Pictorial representation of Waterfall, Iterative and Scrum Software Development methodologies. The waterfall approach is highly risky, often more costly and generally less efficient than more Agile approaches. It requires the creation of up-front documentation before any real business value is created. i.e. You don’t realize any value until the end of the project. This is confounded by the fact that product development is started downstream, or much later in the project’s expected timeframe. This has the obvious disadvantage of delaying the point at which business value can be realized. Remember the 80-20 rule, 80% of a product’s value comes from 20% of its features. Iterative Waterfall Development focuses on delivering a sprint of work as opposed to a series of valuable/shippable features. Scrum Development focuses on delivering fully-tested, independent, valuable, small features. Microsoft Visual Studio Scrum 1.0 Process Template There are two ways to download the template.

What is SCRUM? Contents Introduction SCRUM is a loose set of guidelines that govern the development process of a product, from its design stages to its completion. It aims to cure some common failures of the typical development process, such as: Chaos due to changing requirements - the real or perceived requirements of a project usually change drastically from the time the product is designed to when it is released. Under most product development methods, all design is done at the beginning of the project, and then no changes are allowed for or made when the requirements change. SCRUM has been successfully employed by hundreds of different companies in many different fields, with outstanding results. You will find many similarities between SCRUM and Extreme Programming, but one of the major differences is that SCRUM is a fairly general set of guidelines that govern the development process of a product. SCRUM Values The SCRUM values are derived from the Agile values of software development. Planning Closure

Code Contracts Part 1 – Introduction | Developer Journey LLC Series Redux I discovered Microsoft Code Contracts several years ago and fell in love with the idea. Code Contracts have helped me to write better software since 2008 so I’d like to share them with you. I did an extensive blog series on this subject several years ago which was quite popular. What is a contract? We all have opportunities to deal with contracts now and again in our personal lives. public int Greet(string[] args) { Console.WriteLine("Hello, {0}!" This simple method exposes a contract to which the two parties, the caller and the called function, must agree. Because of the contract, callers can’t pass an array of integers to this function. Can the caller pass null (Nothing) for the args parameter? Well, it looks like we’re missing a few details. Updated Greet Function That Checks Some Pre-conditions We call these kinds of defensive statements guarding because they act like a shield, keeping us from making obvious mistakes inside the function. Introducing Code Contracts

Contracts Code Contracts provide a language-agnostic way to express coding assumptions in .NET programs. The contracts take the form of preconditions, postconditions, and object invariants. Contracts act as checked documentation of your external and internal APIs. Code Contracts bring the advantages of design-by-contract programming to all .NET programming languages. Improved testability each contract acts as an oracle, giving a test run a pass/fail indication.automatic testing tools, such as Pex, can take advantage of contracts to generate more meaningful unit tests by filtering out meaningless test arguments that don't satisfy the pre-conditions. Static verification We have prototyped numerous static verification tools over the past years. API documentation Our API documentation often lacks useful information. Our solution consists of using a set of static library methods for writing preconditions, postconditions, and object invariants as well as three tools:

MSDN Magazine: Cutting Edge - Code Contracts Settings in Visual Studio 2010 Last month I introduced software contracts as they’re implemented in the Microsoft .NET Framework 4. Known as Code Contracts, software contracts allow you to express formal conditions your code should fulfill to work properly. Code Contracts cause your code to throw an exception if a method doesn’t receive data as expected or if it produces data not following expected postconditions. For an overview of preconditions and postconditions, you might want to check out my article in last month’s issue ( Code Contracts are part of the .NET Framework 4, but also rely on a few facilities in Visual Studio 2010, such as runtime tools, integration with MSBuild and a property page in the Project Properties box. In this article, I’ll discuss the intended purpose of the various options you can check or select and drill down into the rewriter tool and practices for the most common thing you do with Code Contracts: argument validation. Runtime Checking Assembly Mode

MSDN Magazine: Cutting Edge - Give Your Classes a Software Contract An old but good practice of software development recommends that you place at the top of each method—before any significant behavior takes place—a barrier of conditional statements. Each conditional statement checks a different condition that input values must verify. If the condition isn’t verified, the code throws an exception. This pattern is often referred to as If-Then-Throw. But is If-Then-Throw all that we need to write effective and correct code? Is it sufficient in all cases? The notion that it may not be sufficient in all cases isn’t a new one. I believe that nearly every developer would agree in principle that a contract-first approach to development is a great thing. Reasoning About a Simple Calculator Class Code Contracts are a state of mind; you shouldn’t put them aside until you’re called to design a big application that requires a super architecture and employment of many cutting-edge technologies. Figure 1 The Calculator Class Implementing the If-Then-Throw Pattern