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
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:
Math.NET Numerics 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]
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.
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
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
.Net Mocking Frameworks - Capability Comparison | PHP vs .Net I have a couple years of experience of TDD under my belt, but it's only recently that I've felt like I am a relatively decent practitioner of it. I attribute this to forcing myself to take the plunge into mocking, and the knowledge of patterns and loosely-coupled design that I've gained from it. You see, I work on a pretty large and complex ASP.Net webforms product, and tests were introduced late into the development cycle of the initial release. We favored integration testing with real data sources over actual unit tests. Recently we put together a public facing API for programming against the product, that we were able to build from scratch. Several weeks down the road, it was obvious that Rhino just isn't right for our environment. This led me on a frustrating research mission to find the differences between frameworks without actually trying them all. (Just for interest's sake -- we ended up choosing Moq, due to the ease of the API. .Net Mocking Framework Comparison Other Notes
Updated TDD Productivity Plug-in for Resharper - Eric Hexter - I first want to thank JetBrains for being pretty awesome. I have complained a lot about how they are constantly chaining their APIs to Resharper and as a result it makes keeping plugins very hard to maintain but they went way out of their way to help. I received and email from one of their developers offering to help on my plugin. Their was a change made in the latest version of resharp which made my plugin incompatible and their telemetry showed them a pattern with this problem. Just this week they made a commit to the source code and updated the project. I did not have the time to get into the internals of this change and I was really motivated by their help. As a result, if you were using the plugin I recommend you download the latest version and install it! Download it Here For those of you who do not use it yet I will run down the features that are available. 1. When prompted with Quick Fixes for a non-existent class You get the following menu. After selecting a menu item. 2.
Fun With .NET Regex Balancing Groups Wednesday, January 23rd, 2008 • Related • Filed Under The .NET Framework's regular expression package includes a unique feature called balancing groups, which is a misnomer since although they can indeed be used to match balanced constructs, that's not all they're good for and really has nothing to do with how they work. Unfortunately, balancing groups are quite poorly documented. Following is a brief description of their functionality, but this post will mostly focus on examples of using them in interesting ways. Note: If you're reading this in a feed reader or aggregator, see the original post, which uses regex syntax highlighting to hopefully make things easier to follow. (? I'm not a .NET coder, but I recognize the potential of this functionality. First, here's a simple example of using balancing groups outside the context of recursion and nested constructs. ^(? A few notes about the above regex: (? ^(? Here's a way to match palindromes (e.g., "redivider"): (? \( (? << (? (?