Thoughts on Technology, Methodology and Programming.

RSpec specifications in C#

Posted by Marcus Wyatt on 13 September 2006

Scott Bellware posted a very interesting article around using C# 3.0  Extension Method to create the same effect as Ruby Mixin to extend existing objects with new methods.

Basically, what I’m talking about is the ability to add a method to an object within a specific context without accessing the sourcecode. For example, add a ToCamelCase() method to the string object in .NET.

This may sound like a bit of voodoo and for someone who’s only ever coded in an strongly typed language, it will definably look that way. But dynamic languages like Ruby and Small talk has always had this behavior.

“Enough rambling man, show us some code”

Here is some of Scott’s examples:

NSpec in C# 2.0 NSpec in C# 3.0 with NSpec.Extensions
Specify.That(actual).ShouldEqual(expected) actual.ShouldEqual(expected)
Specify.That(actual).ShouldBeTheSameAs(expected) actual.ShouldBeTheSameAs(expected)
Specify.That(actual).ShouldNotBeTheSameAs(expected) actual.ShouldNotBeTheSameAs(expected)
Specify.That(value).ShouldBeTrue() value.ShouldBeTrue()
Specify.That(value).ShouldBeFalse() value.ShouldBeFalse()
Specify.ThrownBy(method) method.ThrownBy()
Specify.That(actual).ShouldBeOfType(expectedType) actual.ShouldBeOfType(expectedType)

Here’s Scott’s example of an NSpec specification written in C# 3.0 that used the extension methods. 

using NSpec.Framework;
// Import extension methods for NSpec
using NSpec.Extensions;
namespace Examples
    public class ExampleContext
        public void A_string_should_equal_another_string_when_both_strings_have_the_same_value()
            string s = "foo";

This is really cool, because now you can write your expectations naturally on the object or property that you want to validate.

tags: , , , ,

Currently listening to: Winamp *** 434. atb – drum n bass and beyond – prime


5 Responses to “RSpec specifications in C#”

  1. mawi said


    I love nspecify. I wish boo worked in visual studio, then we could all just use specter. In the meantime, we really need some meaningful failure messages from the specifications.

    And why not incorporate extension methods in an optional nspecify.mixins.dll? Would be awesome.

    Really like the stuff, may consider giving some time.

  2. Very nice! I had a look at how it would look in C#3.0 if spec blocks would be expressed in block form much like in ruby. Well, you may get something like this:

    Context("some context",
    ctx => {
    ctx.Specify("should be nice to check something here",
    () => {
    // Do some spec'ing

    Quite ugly, really 🙂 The Attribute approach looks certainly better. Context and Specify attributes should have a description string for input, though, such that it looks like:
    [Specification(“A string should equal another string when both strings have the same value”)]

    The Extension methods are really sweet and lend themselves excellently for the RSpec approach

  3. […] Extension Methods: Scott bellware comes up with the idea of using C# 3.0 extension methods for an easier expectation syntax. […]

  4. Ecko said

    thanks for sharing…

  5. Good article , thanks for the examples

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: