Category: Testing

C# 4.0 Named Arguments – example usage

Posted by – August 24, 2010

One of the new features introduced in C# 4.0 were named arguments. There was a lot of discussion in the Internet about them and theirs pros and cons. Of course named arguments can be abused like any other part of C# but I think that when used wisely are able to make your code more readable and easier to understand without the knowledge what is hidden behind the scene. Few days ago I was working on integration tests for REST API exposed by a startup I am working on with my team. Each test required some “execution context” and a way to inform “infrastructure” that after execution whole environment should be cleaned up (e.g. database). Because I use NUnit as a runner the first idea that came to my mind was to utilize [SetUp] and [TearDown] methods. But after some prototyping I realized that the solution is hard to understand and obfuscates the tests. Next day after short chat with @lesnikowski new idea popped into my mind: I can create “execution context” with lambdas and named arguments! The example below shows how I have changed the idea into real code.

[Test]
public void Add_WhenInvokedShouldAddNewExternalAudioFolder()
{
    Execute(test: () =>
    {
        var json = WebApiClient.ExternalAudioFolder.Add("My folder", "My description");
        var message = json.DeserializeJson<AddMessage>();

        Assert.That(message.Result, Is.EqualTo(Message.Success));
    },
    inContextOf: Account.Basic,
    cleanUpAfter: true);
}

What do you think about the code above? Do you have any additional ideas how to improve readability of this “execution context”?

MSpec BDD framework installer

Posted by – November 23, 2009

.NET LogoInstallation of MSpec BDD framework from source code is quite annoying. With each release you have to deploy everything manually one more time what in fact is hard to accept in 21th century. I know that work on Open Source project requires a lot of time (I have my own project called ByteCarrot) and you cannot do everything. Because of that I decided to help a little bit creators of MSpec and I have prepared an installer for this BDD framework. It is based on WiX and latest release of MSpec witch is version 0.3.

The installer is able to automatically integrate MSpec with TestDriven.NET and ReSharper (4.1, 4.5, 5.0). This is first version of the installer and of course like always there can be some bugs so please let me know if you find something.

Download installer for MSpec 0.3

ReSharper’s Live Templates for MSpec BDD framework

Posted by – September 23, 2009

Few days ago Pawel Lesnikowski has blogged about his Live Templates for ReSharper. Because I think it is good idea to share such things with other developers I decided to show my Live Templates I have made to be able to create BDD specifications with MSpec faster. Here they are:

specc – Short template for MSpec BDD context

public abstract class with_$Context$
{
	Establish context = () =>
	{
		$END$
	};
}

specf – Full template for MSpec BDD specification

[Subject(typeof($Subject$))]
public class when_$Specification$
{
	Establish context = () =>
	{

	};

	Because of = () =>
	{

	};

	It should_$Behaviour$;$END$
}

specs – Short template for MSpec BDD specification

[Subject(typeof($Subject$))]
public class when_$Specification$
{
	It should_$Behaviour$;$END$
}

spect – Default template for MSpec BDD specification

[Subject(typeof($Subject$))]
public class when_$Specification$ : with_$Context$
{
	Because of = () =>
	{

	};

	It should_$Behaviour$;$END$
}

Here you can download definition of all mentioned templates which can be imported to your ReSharper: MSpec.LiveTemplates