Doing test driven development with dnx-watch

Introduction

I recently attended a software craftsmanship event in Belgium. It was a great experience and an interesting moment to look over the fence to some other programming languages.

I've been a .NET developer for some time now. And about a year ago I discovered the joy of test driven development (TDD). But for some reason I have a feeling that TDD is something the average .NET developer isn't going to do spontaneously.

Well it kinda takes a while

There is a bit of a hassle to start TDD with Visual Studio. You need another project, download a framework of choice, most likely a mocking framework as well, get to run the tests, ... etc. Now I know it doesn't have to be this way, but it will most likely be.

At the Socrates event I saw people coding in Ruby and Java really spin up tests very fast. Might have been their JetBrains IDE but it just seemed like a natural thing to do.

Some even refused to write anything else then a failing test.
As it should be according to the three laws of TDD.

The problem with doing this in Visual Studio is that it just takes a while to compile your code.
It gives an impression that it slows you down.

Enter DNX-watch

With the release of beta 8 the ASP.NET team created a file watcher that will run dnx commands whenever a project file changes.

Two great use cases are restarting your webserver and running unit tests.
I've created demo application where I write the classical bowling game in a TDD way.
As I saved my ∗.cs files, the console immediately runs my tests.

DNX-watch is a wonderful experience when doing TDD

The bowling demo

So the demo project isn't rocket science to set up or to solve. It's been done over and over again and I just want to express the joy of creating it.

Setup

I've created two projects:

  • BowlingVNext
  • BowlingVNext.Tests

The first contains a game class with has one method Roll(int pins) and one function Score().

The second projects contains the tests and uses xUnit.
And of course it references the first project.

project.json of the test project:

  ...
  "commands": {
    "test": "xunit.runner.dnx"
  },
  "dependencies": {
    "BowlingVNext": "1.0.0-*",
    "xunit": "2.1.0",
    "xunit.runner.dnx": "2.1.0-beta6-build191"
  }
  ...

After a dnu restore and dnu build we can start watching our test project by using
dnx-watch test

Unit tests, production code and console

The Source

Available on github

Remarks

  • If you don't know xUnit, you should really check it out. It's easy to learn.
  • I'm no expert in ASP.NET 5, but with dnx-watch I might be hooked.
  • The screenshot contains certain words in red. This is an editor problem I assume. Could be a Resharper thing as well.

Final words

In the future I hope to do some more posts on ASP.NET 5. I've always been enthusiastic on this rewrite and I'm curious to see where it goes. If you have any requests just ping me. Happy testing.

Yours truly,
nojaf

Florian Verdonck
Florian Verdonck

Florian Verdonck is web & .NET developer. He is passionate about clean code and is eager to learn new technologies.