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.
REST Anti-Patterns When people start trying out REST, they usually start looking around for examples – and not only find a lot of examples that claim to be “RESTful”, or are labeled as a “REST API”, but also dig up a lot of discussions about why a specific service that claims to do REST actually fails to do so. Why does this happen? HTTP is nothing new, but it has been applied in a wide variety of ways. Some of them were in line with the ideas the Web’s designers had in mind, but many were not. Applying REST principles to your HTTP applications, whether you build them for human consumption, for use by another program, or both, means that you do the exact opposite: You try to use the Web “correctly”, or if you object to the idea that one is “right” and one is “wrong”: in a RESTful way. As with any new approach, it helps to be aware of some common patterns. Let’s start with a quick list of anti-patterns I’ve managed to come up with: Let’s go through each of them in detail. Tunneling everything through GET
Math.NET Numerics Rahien - Rhino Mocks In my previous post, I started to go over the go-raft implementation, after being interrupted by the need to sleep, I decided to go on with this, but I wanted to expand a bit first about the issue we discussed earlier, not checking the number of bytes read in log_entry’s Decode. Let us assume that we actually hit that issue, what would happen? The process goes like this, we try to read a value, but the Read method only return some of the information. We explicitly ignore that, and try to use the buffer anyway. I reported this issue, and I’m interested to know if my review is in any way correct. getEntriesAfter gives us all the in memory entries. setCommitIndex is interesting. And this one gives me a headache: Basically, this mean that we need to write the commit index to the beginning of the file. The truncate method makes my head ache, mostly because it does things like delete data, which makes my itchy. Then we have this: This seems pretty easy to follow, all told. Flush is defined as:
Introducing BDD « DanNorth.net 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:
State Testing vs Interaction Testing Highly recommended reading: Fowler’s article Mocks aren’t Stubs. It’s a very clear characterization of the types of so-called Test Doubles (mocks, fakes, stubs, etc.) you can use to aid your unit testing needs, but also of the kinds of TDD you can do: classic (or state) TDD and mockist (or interaction) TDD. I got the feeling that the article seemed a bit biased towards mockist TDD, only to see in the “So should I be a classicist or a mockist?” section that Fowler himself is a classic TDDer ). Maybe it’s because mockist TDD is a newer approach to TDD and therefore required a more extensive explanation. I’m a state testing guy myself, but I always found it very annoying having to create all those little “mocks” to help me test classes that had dependencies. We suffered dozens of tests fail not only as a consequence of refactoring, but also as part of regularly added new features (i.e. a new event being raised by a collaborator). .
Best and Worst Practices for Mock Objects - Jeremy D. Miller -- Mock objects are like any other tool. Used one way they’re a huge time saver. In other cases they can easily create more work and degrade the quality of the testing code. This is the third in a planned series of five posts on mock objects: Introduction to Mocks and StubsWhen and Why to use Mocks and Stubs Best Practices for Mocks (this post)Constraints and other Mocking Tricks Attaching Mocks and Stubs with StructureMap (by request) Mocking Interfaces and Classes outside Your Code Be careful about mocking or stubbing any interface that is outside your own codebase. The best example from my memory was a security subsystem that retrieved security authorization and user information from Active Directory. Instead of trying to mock the access to Active Directory, create your own coarse-grained interface for retrieving user information that will encapsulate the AD access. public interface IUserInformationStore UserInformation FindUser(string userName); Don’t mock fine grained interfaces. [Test]
markdownsharp - Project Hosting on Google Code Open source C# implementation of Markdown processor, as featured on Stack Overflow. This port is based heavily on the original Perl 1.0.1 and Perl 1.0.2b8 implementations of Markdown, with bits and pieces of the apparently much better maintained PHP Markdown folded into it. There are a few Stack Overflow specific modifications (which are all configurable, and all off by default). I'd like to ensure that this version stays within shouting distance of the Markdown "specification", such as it is... You'll need to know: C# Regular Expressions (I mean really freaking know them -- the original Markdown implementation was in Perl. If you'd like to contribute file issues and attach *.patch files of proposed fixes. Please note that as of January 8, 2011 MarkdownSharp uses Mercurial for version control; the old Subversion repository will not be updated anymore. Tests I pulled in the MDTest 1.1 test suite, along with some very rudimentary unit tests of my own. Benchmarks Markdown.NET MarkdownSharp v1.12
Integration Testing Your ASP.NET MVC Application « Steve Sanders Let’s start with a quick recap: Unit tests test individual software components. They supply mocks or fake versions of dependencies (such as a database) so that the unit test doesn’t rely on any external code and any failures can be pinpointed exactly. ASP.NET MVC famously has great support for unit testing. Typically, ASP.NET MVC developers have written integration tests using a browser automation tool, such as Selenium or Microsoft’s Lightweight Test Automation Framework. Browser automation test scripts are very fragile. So, how can we do integration testing without browser automation? Introducing MvcIntegrationTestFramework What if you could write NUnit tests (or xUnit or whatever you prefer) that directly submit a URL, query string, cookies, request headers, etc., into your application – without needing the app to be hosted in any web server but still running in the real (non-mocked) ASP.NET runtime – and get back the response text to make assertions about? Download the demo project.
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!
Visual Studio 2010: Test Driven Development Description Visual Studio 2010 brings with it several enhancements to help cut development friction and enable the developer to focus on the task at hand: writing high-quality code. In the following exercises we'll highlight several of the new features that the TDD developer can use to enhance his/her development cadence. Visual Studio helps your cadence by cutting the number of keystrokes to accomplish frequently performed tasks, speeds navigation through your solution, and enables you to use test frameworks other than MSTest. Overview Test Driven Development (TDD), also frequently referred to as Test Driven Design, is a development methodology where developers create software by first writing a unit test, then writing the actual system code to make the unit test pass. TDD follows a cadence of “Red, Green, Refactor.” Note: This lab is not meant to teach you how to work with TDD; instead, it is intended to highlight Visual Studio 2010’s support for working in TDD. Objectives Setup Exercises