Pumping Iron – Ruby & .NET testing
After being inspired by Ben Hall’s “Testing C# and ASP.NET using Ruby” session at DDD8, I decided to give Ruby testing a go.
This article shows the basics of how to get started, for the added cool-ness of BDD and Cucumber check out Bens slides from the talk.
What do I need?
IronRuby
IronRuby is Ruby for the .NET platform build on the DLR which allows you to use the Ruby language to interact with your existing .NET code.
RubyMine
RubyMine is a very cool IDE for developing Ruby applications that Ben used in his DDD demo, there are many other Ruby IDEs available including TextMate for the Mac and RadRails for Eclipse.
Now that we have all the none .NET pre-reqs downloaded and installed we can get to business.
Create Application Under Test
First things first, lets set up a skeleton application in Visual Studio that we will test. Here I just created a blank class library called IronRubyMine with the Person class within it. Build this code so that we have a dll in the bin/Debug directory.
namespace IronRubyMine
{
public class Person
{
}
}
Setting up RubyMine for IronRuby
Within RubyMine I have then created a project in the same directory as my sln file for C# application, this will generate all your Ruby classes in the same location as your .NET app.
Once we have the Ruby solution we need to do a few tweaks to get it to work smoothly with .NET. Start off by adding the IronRuby SDK for use with the project (File – Settings – Ruby SDK – Add SDK…) and browse to the location of your IronRuby install and the ir.exe file.
Now RubyMine is using the IronRuby SDK we need to make another minor tweak to the Run/Debug configuration the files in the project. You will need to alter the Ruby Arguments to:
-e STDOUT.sync=true;STDERR.sync=true;load($0)
There are a number of discussions on the RubyMine forums how to best do this, but I have found that this alteration is the easiest and most straight forward way to get it all working.
Red – Create Failing Test
With our environments set up we create a simple test that fails before we write the code that we want and make the test pass.
require "test/unit"
require "IronRubyMine/bin/Debug/IronRubyMine.dll"
class PersonTest < Test::Unit::TestCase
include IronRubyMine
def test_create_person_called_dominic
dominic = Person.new("Dominic")
assert_not_nil(dominic)
end
end
The second line pulls in the .NET dll that we created in our app allowing IronRuby to call any classes and methods that we create in our C# code. This is then used further down to create a Person object.
On running this test we will get a red light as we haven’t yet implemented the C# code to go alongside the test.
Green – Make Tests Pass
Now we can go back to Visual Studio and create a Person constructor that will take in a name, hopefully making our tests go green.
Once we have the code in place, build the application to ensure that the dll is updated and our IronRuby code is using the latest implementation.
Now jump back into RubyMine and re-run the tests.
Refactor…
You get the picture, with all our tests now passing we can do any refactoring needed and then go on to create more tests.
With this simple implementation in place we can build on this to implement more advanced techniques described in Bens presentation.