Ivan Porto Carrero

IO(thoughts) flatMap (_.propagandize)



A Rant on a Talk Falling to Pieces

Earlier this week I had the pleasure of doing a talk for the Belgian .NET user group. During this talk I ran in to all kinds of problems. I’ve done presentations where I was royally underprepared and to avoid that this time I actually started prepping for this talk on time. I was done on time, was prepared had 5 backups of my presentation and samples. What did I learn from this, if you’re prepared other things will go wrong.
I’ll share the story of stuff that can go wrong.

Before starting the rant I have some links to share.

I have a device that I call internet on a stick, which is a vodafone usb 3G modem that I plug in to my system and it gets me on the internet (mistake 1). Since I assumed that that thing would continue to work I made most of my demos internet enabled (mistake 2). For example I have a demo where I go download pics from flickr and then show them with some animations with silverlight.
Before my presentation I changed the fonts, opened all the files I was going to talk about ran all the demos again to make sure they would work and everything went fine.
I unplug the USB device and go into the room to hook up the projector etc. The presentation starts and for the first hour everything went really well (from my perspective at least don’t know about the people attending).
We have a break and I plug the USB device in, at this point the vodafone program hangs (first time ever I swear). What’s more I can’t make it go away at all so I reboot my pc (this is still during the break).

Now I’m getting a little desperate because it still doesn’t work. With rebooting I also lost all my carefully opened files earlier (I’m showing code in about 3 different environments and 2 different OS’es).
So during the presentation I apologise and try to reboot once more while taking questions from the audience and hoping somebody will try to start a discussion with me. After rebooting I got a message saying my date was set to 2001 which I thought was peculiar but clicked it away. I boot vmware fusion with windows 7 (this took fairly long and is a little bit funky as the screen resolution changes a lot during this process).
Ok so far so good, by now I’ve already skipped the silverlight demo promising that it will be available as a download on my blog and will be moving on to the ironrubymvc sample. To prove I do actually use visual studio at times I wanted to open my demo project in visual studio. I open visual studio only for it to tell me that my trial has expired and I can either upgrade or close the application. Oooooookay this is completely weird because i get my software through my msdn subscription and I had been using it earlier that day.

Moral of the story: Either go vastly underprepared and wing it. Or don’t rely on the internet and always take at least 2 laptops that have identical configurations but I’m pretty sure those would or explode in my face or something will fall from the ceiling, building collapses or other mishaps.

Instead of having one demo go bad on me now I’m probably facing a reinstall of my mac because it lost a bunch of settings, for which I’m holding the vodafone responsible. At this moment I’m fairly certain that I should go less prepared and just wing it just out of fear for bigger disasters, people may die.



Very Simple IronRuby MVC Sample Application

Over the weekend I wrote a very simple task list application with IronRuby MVC and LightSpeed as an OR/M. And I’ve just put a demo live. This demo is running on a windows 2008 vps with IIS 7 as webserver. The source of this sample is also available.

At this moment I’m working on the Ironruby mvc sample for my book. And while I was playing around to get some samples together I tried building the simplest application possible that has some degree relations in the db and does some crud. And I came up with the original idea of creating a task list application.

As an OR/M I choose to use LightSpeed and it runs on a sqlite database so it should be immediately usable after checking out. There is one caveat though. If you’re using a 32-bit OS you’ll need to replace the sqlite dll that is included in the dependencies tree with a 32-bit version of the dll and rebuild the solution.

The links:


Source of sample:

IronRubyMVC source:



Technorati Tags: IronRuby MVC,IronRuby,ASP.NET MVC



Getting Started With Caricature and IronRuby

IronRuby 0.5 was released yesterday. You can download it on codeplex. This post will explain how you setup your ironruby environment to use it for testing existing CLR based assemblies. We’ll touch installing gems using rake and most importantly writing a test for a CLR based class where we’ll mock out the dependencies.

The first thing you need to do is download IronRuby. After downloading you can extract it to a location on your hard drive. I extracted mine to C:\ironruby

Now we need to add the path to ir.exe to our PATH variable so we can use it from the command-line. And that is all there is to it to install ironruby on your machine. Now we need to get the necessary gems onto our system. We’ll need bacon and caricature.

Open a console and type the following:

igem list will show you a list of the gems you have installed on your system.

igem install will fetch and install a gem on your system. To install the gems we’re going to need we need to execute the command

igem install bacon caricature

That will result in the following output:

+ C:\tools
» cmd
Microsoft Windows [Version 6.1.7100]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\tools>where ir

+ C:\tools
» igem list

*** LOCAL GEMS ***

+ C:\tools
» igem install bacon caricature
Successfully installed bacon-1.1.0
Successfully installed caricature-0.6.0
3 gems installed
Installing ri documentation for bacon-1.1.0...
Installing ri documentation for caricature-0.6.0...
Installing RDoc documentation for bacon-1.1.0...
Installing RDoc documentation for caricature-0.6.0...

If you would try to execute ibacon at this point that would work. We’ll need to create 2 small files to get ibacon to work. In the bin directory of ironruby I created 2 files one called ibacon and the other one is ibacon.bat

# This file was generated by RubyGems.
# The application 'bacon' is installed as part of a gem, and
# this file is here to facilitate running it.

require 'rubygems'

version = ">= 0"

if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
  version = $1

gem 'bacon', version
load 'bacon'

@"ir.exe" "%~dpn0" %*

At this point you’re ready to start writing specs with bacon and caricature. Let’s look at an example I wrote for the controller factory for ironruby mvc.

We’re going to test the following C# code.

public interface IWeapon{
    int Attack(IWarrior warrior);
    int Damage();

public interface IWarrior
    int Id { get; }
    string Name { get; set; }
    bool IsKilledBy(IWeapon weapon);
    int Attack(IWarrior target, IWeapon weapon);
    int SurviveAttackWith(IWeapon weapon);

public class Sword : IWeapon

    public int Attack(IWarrior warrior){
        return warrior.SurviveAttackWith(this);

    public int Damage(){
        return 4;

I generally create a bacon_helper.rb file where I group my requires and helper functions etc. In the case of this test I have the following in the bacon_helper.rb:

# add some search paths to ironruby
# this first one adds the path with the assemblies
# this enables us not to have to specify a path to the assemblies everywhere.
$: << File.dirname(__FILE__) + "/bin"
# adds the path to the caricature library.
$: << File.dirname(__FILE__) + "/../lib"

# load the caricature library
require "caricature"
require 'caricature/clr'

# load the bacon library
require 'bacon'

# Add the .NET framework
require 'mscorlib'

# load the assembly with the C# code
load_assembly 'ClrModels'

At this point we’re ready to start writing the test. create a file called sword_spec.rb and we’ll add the following content to the file.

require File.dirname(__FILE__) + "/bacon_helper.rb"

describe "ClrModels::Sword" do

  before do
    @warrior = Caricature::Isolation.for ClrModels::IWarrior

  it "should call survive_attack on the mock" do

    sword = ClrModels::Sword.new
    sword.attack(@warrior).should.equal 5


  it "should return different results when expectation is defined with arguments" do
    sword1 = ClrModels::Sword.new
    sword2 = ClrModels::Sword.new


    sword1.attack(@warrior).should.equal 5
    sword2.attack(@warrior).should.equal 15



So now we’ve got 2 tests for our Sword class. The only thing that is left to do is to run the specs. You can do that by executing the ibacon command and passing it the file you want to test.

+ C:\dev\caricature
(master) » ibacon spec/sword_spec.rb
- should call survive_attack on the mock
- should return different results when expectation is defined with arguments

2 specifications (5 requirements), 0 failures, 0 errors



Caricature Ready for Beta

The last couple of days I’ve been getting Caricature to a more releasable state. The code got a thorough cleanup and refactor. Caricature now knows how to be a full mocking framework for Ruby classes and CLR classes that only interact with ruby objects. When I apply the 80/20 rule to caricature it’s definitely beta worthy.

I’m not a huge fan of the 80/20 rule unless all the features I want are in the 80 part. In that case for me it’s a 100 rule ;).

As for the latest changes:

Adding require 'caricature' only gives you ruby mocking. If you want to use it to mock CLR classes or interact with the CLR you’ll need to add an extra require statement:require 'caricature/clr'. I’ve done it this way so that I can still add Java and MacRuby support later on.

By default caricature will always give you an instance of an isolation, but there are times that you’ll want to substitute the result of a class method invocation. So the API got expanded with 2 methods, when_class_receives and did_class_receive? These methods enable you to setup substitutions for class method calls.

I’m still waiting for IronRuby 0.5 to be released to give you a blow-by-blow tutorial on how to setup your environment.

Basically you download the ironruby release from Codeplex

Then you make sure the path to ir.exe is in your PATH variable

After that you should be able to do igem list and get an empty result back.

now you need to install bacon you can do that by executing

igem install bacon

next you need the caricature gem

igem install caricature

You will also need rake to be installed to build the cs files included in the sources.

igem install rake

ok now you should be good to to

require 'rubygems'
require 'bacon'
require 'caricature'
require 'caricature/clr'

describe "when isolating Ruby classes with class members" do

  before do
    @dagger = Dagger.new
    @soldier = Caricature::Isolation.for(SoldierWithClassMembers)

  it "should work without expectations" do
    result = @dagger.attack @soldier
    result.should.equal nil

  it "should work for expectations with an argument constraint" do
    @dagger.attack(@soldier).should.equal 5


you can get the gem from rubyforge

Or from my github account: http://github.com/casualjim/caricature



An Update on Caricature

Yesterday I explained about caricature but I didn’t have it fully tested yet. Today I’ve written a bunch of integration specs for all the platform combinations Caricature currently supports so you should be able to use Caricature also for plain ruby object mocking in addition to CLR interop mocking.

After having a very brief twitter discussion with Scott I decided it would be a good idea to change the name of the methods when_told_to and was_told_to? . In ruby objects receive messages so the wording now becomes when_receiving and did_receive? . I have a problem with the should naming because there is nothing conditional about it. Either you want the method to return something or you want it to return a default value for CLR value types or nil. And if you’re asserting if a method it called that is deterministic; it is either called or not there is no gray area there.

ninja = ClrModels::Ninja.new


@ninja.attack(ninja, @weapon).should.equal 5


Before I call it 1.0 I want to at least give ruby objects also the ability to isolate static and sealed methods as well as mocking ruby class methods. So that for CLR to Ruby interaction you a get the full range of possibilities.

I have also set up a site on github that contains the API documentation for Caricature. When IronRuby 0.5 releases I’ll publish a blog post that will talk you through testing your CLR assembly with IronRuby, bacon and Caricature.

For now you can take a look here for example on how to use Caricature for isolating features.



IronRuby Just Got a Mocking Framework – Kind Of

As I mentioned in a previous post. I started working on a small mocking framework. It has now progressed far enough to handle the most common mocking tasks.

Below I pasted the output of the integration tests for CLR interop.

when isolating CLR interfaces        
- should work without expectations         
- should work with an expectation with any arguments         
- should work with an expectation getting different method call result         
- should work for an assertion on a specific argument 

when isolating CLR classes        
- should work without expectations         
- should work with an expectation for any arguments         
- should work with an assertion for specific arguments         
- should fail for an assertion with wrong arguments 

when isolating CLR instances        
- should work without expectations         
- should work with an expectation for any arguments         
- should fail for an assertion for specific arguments         
- should allow to delegate the method call to the real instance (partial mock)

you will need bacon installed to run the specs:

igem install bacon

you can then install the caricature gem in ironruby by issueing

igem install caricature

To use it there are some examples in the file spec/integration_spec.rb

require 'rubygems'
require 'bacon'
require 'caricature'


weapon.attack(ninja).should.equal 5


There is a gotcha though, when you use it in a CLR class you’re bound to CLR rules and it only overrides the methods that are marked as virtual. We also can’t isolate static or sealed types at the moment.

I took the approach of doing away with the terminology of mocking and subbing and instead chose the much clearer Isolation. By default any method returns null or the default value of a value type. You can tell an isolation to return a specific value or raise an error etc. Later on you can then assert if the method was actually called.

This fits in better with the way you probably structure your tests.

I hope you like it.

You can find the source in my github account.




Joined the Lounge Network

Today I received my invitation to the Lounge network. I just finished changing my homepage to include their ads in my side bar.
My reasons for joining the ad network are that on one hand I’ve been an ad-sense member for a really long time and although I’ve had ads on multiple sites it never actually made me any money. Eventually I gave up on putting those ads on my blog. So I’m not really doing it to make any money of this blog, but if it takes a small bite out of my hosting bill I’ll be a happy man :).

The ads that are run by the lounge are .net centric and are a much better fit to the audience of this blog. It even may be helpful to somebody at times.

Let’s see how this works out.



Mocking for IronRuby

As you may or may not know I’m in the process of building IronRubyMvc. At one point I did write a bunch of tests in for the code I had at that point. However that test code was written in C# with xunit and moq. I wasn’t too happy about that so I deferred writing tests to a later date when I could use a Ruby library to write them.

That time has come, I can use bacon and it’s acceptable performance wise now, not stellar but workable. So when I started to port some of my previously written tests to bacon I ran into a road block. Mocking isn’t as straightforward as my optimistic self was expecting. This lead me to think about what exactly does a mocking framework do?

I’m pretty much a just stuff no fluff kind of guy when it comes to coding. So I tend to choose for solutions that have just the right amount of features and virtually no whistles and bells. I generally blame it on being slightly stupid so I can’t grok intensely complex solutions. Once you cross a treshold there is no way in hell I will ever look at it again.

Anyway the no fluff approach to mocking would be to look at the core responsibilities of those frameworks, which are 2 as far as I can tell.

  1. Generate a proxy for an object so you can intercept method calls
  2. Allow for verificatons of those intercepted method calls to do things like how many times was this method called, was it called at all etc.

So I’m starting to implement a way to generate proxies today.. This should be a piece of cake with Ruby :) Then I’ll look at implementing some constraints and verifications.

Thinking about how to approach the API for defining the mocks has also been an interesting experience. From the get-go a Record/Replay/Verify model is out. But then it comes to the following part, when do you define the verifications.

Do you do that in the assertion fase, thus stubbing everything beforehand and just allowing the user to define return values when establishing the test context. Or do you define the verification (at least twice, ignore arguments etc) when you establish the test context. My vote goes to the first option.

So a new project is born it’s called caricature and lives on github. Which will try to implement the ideas set forward in this blog post for IronRuby.


Technorati Tags: IronRuby,Mocking



Using IronRuby With RubyMine

RubyMine was released earlier this week. I’ve used rubymine in the past and was very impressed with the IDE, so much that I’m silently hoping the guys from Jetbrains would do a C# IDE too. I’d probably buy that one as well, but for now I’ll settle for Resharper with Visual studio.

By default Rubymine won’t accept IronRuby as a ruby SDK, but I asked for help in the twitterverse and got it (http://twitter.com/iRomeo/statuses/1677867948).


Just symlinking didn’t really work because the ruby.exe listens to –version for getting its version number.  IronRuby on the other hand listens to –v to get its version number.  The solution to that is to create a batch file that will translate the –version  modifier to –v.

So instead of symlinking the ir.exe to a name that starts with ruby I created a batch file called ruby_iron.cmd with the following content.

@echo off
set IR_CMD="%~dp0ir.exe"
if "%1"=="--version" ( GOTO RUBY_VERSION )
%IR_CMD% -v
goto QUIT

With that ruby_iron.cmd file in place you can just add IronRuby as an SDK in RubyMine.

Enjoy :)



Binder for Ironruby Mvc

I’ve progressed far enough with IronRuby MVC to start thinking about a sample and I’ve decided to write 2 separate samples both on the same technologies. As an OR/M I will be using LightSpeed from Mindscape, just because it’s an awesome piece of technology.

At this moment I’m looking at creating a binder for objects that takes a hash as input. The default model binder of ASP.NET MVC doesn’t quite fit all that well with the ironruby implementation so I’m creating a ruby binder implementation. This stuff is just something that makes ruby shine.

While doing so I came up with this little tidbit of code:

class System::Object

  class << self

    def create_from_hash(options)
      result = self.new
      result.populate_from_hash options


  def populate_from_hash(options)
    options.each do |k, v|
      mn = "#{k}=".to_sym
      self.send(mn, v) if self.respond_to?(mn)

This code allows you to populate any .NET object from a hash.

So doing Person.create_from_hash(:username => “joe”) would create a user instance with the property username set to joe.

You do need to provide a hash where the values have the correct type.

Technorati Tags: IronRuby MVC,IronRuby

To top