White chocolate ruby mocha, please

In recent weeks, I’ve been having fun with Ruby at work. As part of an effort to refactor some of our old hacked-together-in-a-day ruby scripts, I’ve been extracting the various concepts dispersed into multiple files called via system calls from a main script file.

system(ruby, "SuperHackyScript.rb")

To ensure that I did not break anything as part of this refactoring effort, I started putting tests around the existing functionality I was porting over. Due to the nature of the scripts, writing full integration tests would have been far too costly, which is why I started looking for mocking framework in Ruby that would closely resemble what I already do with Moq or Rhino Mocks. That is how I found out about Ruby Mocha.

Let’s say that for whatever reason I needed to make calls to git as part of one of my scripts. There’s a pretty good Git wrapper out there called Ruby-Git that I would recommend, especially for those using Windows since Grit doesn’t work on non-Unix systems.

For testing purposes, I don’t acutally want to be making git calls, which is why I would want to use a mocking framework such as Mocha to stub that out.

In a real implementation, you likely wouldn’t want to be raising runtime errors like it’s nobody’s business. A better approach would be to create a data class such as an ‘OperationResponse’ whose properties would include a boolean stating wheter the operation succeeded or not, as well as the failure reason if the operation failed.

Word of warning

Notice that to create my strict mocks, I just new up a Mock object, without any sort of type constraint. For my partial/loose mocks, I create the type as I would normally, and call the expect method, as if it was a C# extension method.

Dynamically typed languages are interesting in that you do not know the type of the objects being passed in until you evaluate/interpret them. This can lead to bugs in your code base that would have been caught as compilation failures had you been using a statically typed language. Of course, proper unit testing could go a long way in reducing such issues…but you are still exposed to such risks.

Because Ruby is a dynamically typed language, I was curious as to how Mocha would be able to handle my mocked expectations. It’s not as if it could know the true type of the values I’m passing in. As such, it has no way of knowing for sure that variable ‘git’ is of type ‘Git’, and has a method called ‘commit’…or that it **lacks** such a method. Unfortunately I couldn’t find a way to constraint the type, but the framework is nonetheless useful and worth digging through.

White chocolate ruby mocha, please

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s