<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ivan Porto Carrero &#187; Mocking</title>
	<atom:link href="http://flanders.co.nz/category/mocking/feed/" rel="self" type="application/rss+xml" />
	<link>http://flanders.co.nz</link>
	<description>thoughts.each { &#38;:propagandise }</description>
	<lastBuildDate>Sat, 03 Sep 2011 09:56:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Getting started with Caricature and IronRuby</title>
		<link>http://flanders.co.nz/2009/05/21/getting-started-with-caricature-and-ironruby/</link>
		<comments>http://flanders.co.nz/2009/05/21/getting-started-with-caricature-and-ironruby/#comments</comments>
		<pubDate>Thu, 21 May 2009 13:52:17 +0000</pubDate>
		<dc:creator>Ivan Porto Carrero</dc:creator>
				<category><![CDATA[caricature]]></category>
		<category><![CDATA[IronRuby]]></category>
		<category><![CDATA[Mocking]]></category>

		<guid isPermaLink="false">http://flanders.co.nz/2009/05/21/getting-started-with-caricature-and-ironruby/</guid>
		<description><![CDATA[<a href="http://flanders.co.nz/2009/05/21/getting-started-with-caricature-and-ironruby/" title="Getting started with Caricature and IronRuby"></a>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.&#160; We’ll touch installing gems using rake and most importantly writing &#8230;<p class="read-more"><a href="http://flanders.co.nz/2009/05/21/getting-started-with-caricature-and-ironruby/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://flanders.co.nz/2009/05/21/getting-started-with-caricature-and-ironruby/" title="Getting started with Caricature and IronRuby"></a><p>IronRuby 0.5 was released yesterday. You can download it on <a href="http://ironruby.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=25902#DownloadId=68919" target="_blank">codeplex</a>. This post will explain how you setup your ironruby environment to use it for testing existing CLR based assemblies.&#160; 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.</p>
<p> <span id="more-351"></span>
<p>The first thing you need to do is download <a href="http://ironruby.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=25902#DownloadId=68919" target="_blank">IronRuby</a>. After downloading you can extract it to a location on your hard drive. I extracted mine to C:\ironruby</p>
<p>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.</p>
<p>Open a console and type the following:</p>
<p>igem list will show you a list of the gems you have installed on your system.</p>
<p>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</p>
<p>igem install bacon caricature</p>
<p>That will result in the following output:</p>
<pre>

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

C:\tools&gt;where ir
C:\ironruby\bin\ir.exe

C:\tools&gt;exit
+ 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...
</pre>
<p>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.&#160; In the bin directory of ironruby I created 2 files one called ibacon and the other one is ibacon.bat </p>
<p>ibacon</p>
<pre class="ruby" name="code">

#!C:/ironruby/bin/ir.exe
#
# 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 = &quot;&gt;= 0&quot;

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

gem 'bacon', version
load 'bacon'
</pre>
<p>ibacon.bat</p>
<pre name="code">

@ECHO OFF

@&quot;ir.exe&quot; &quot;%~dpn0&quot; %*
</pre>
<p>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.</p>
<p>We’re going to test the following C# code.</p>
<pre class="csharp" name="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;
    }
}
</pre>
<p>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:</p>
<pre class="ruby" name="code">

# 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.
$: &lt;&lt; File.dirname(__FILE__) + &quot;/bin&quot;
# adds the path to the caricature library.
$: &lt;&lt; File.dirname(__FILE__) + &quot;/../lib&quot;

# load the caricature library
require &quot;caricature&quot;
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'
</pre>
<p>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.</p>
<pre class="ruby" name="code">

require File.dirname(__FILE__) + &quot;/bacon_helper.rb&quot;

describe &quot;ClrModels::Sword&quot; do

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

  it &quot;should call survive_attack on the mock&quot; do
    @warrior.when_receiving(:survive_attack_with).return(5)

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

    @warrior.did_receive?(:survive_attack_with).should.be.successful
  end

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

    @warrior.when_receiving(:survive_attack_with).return(5)
    @warrior.when_receiving(:survive_attack_with).with(sword2).return(15)

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

    @warrior.did_receive?(:survive_attack_with).with(sword2).should.be.successful
  end

end
</pre>
<p>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.</p>
<pre name="code">

+ C:\dev\caricature
(master) » ibacon spec/sword_spec.rb
ClrModels::Sword
- 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
</pre>
<p><div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e7a7ff6b-7e8d-4103-aea9-665298fa5cde" style="float:none; display:inline; margin:0px; padding:0px 0px 0px 0px;">Technorati Tags: <a href="http://technorati.com/tags/ironruby" rel="tag">ironruby</a>,<a href="http://technorati.com/tags/caricature" rel="tag">caricature</a>,<a href="http://technorati.com/tags/mocking" rel="tag">mocking</a></div></p>
]]></content:encoded>
			<wfw:commentRss>http://flanders.co.nz/2009/05/21/getting-started-with-caricature-and-ironruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Caricature ready for beta</title>
		<link>http://flanders.co.nz/2009/05/19/caricature-ready-for-beta/</link>
		<comments>http://flanders.co.nz/2009/05/19/caricature-ready-for-beta/#comments</comments>
		<pubDate>Tue, 19 May 2009 14:06:04 +0000</pubDate>
		<dc:creator>Ivan Porto Carrero</dc:creator>
				<category><![CDATA[caricature]]></category>
		<category><![CDATA[IronRuby]]></category>
		<category><![CDATA[Mocking]]></category>

		<guid isPermaLink="false">http://flanders.co.nz/2009/05/19/caricature-ready-for-beta/</guid>
		<description><![CDATA[<a href="http://flanders.co.nz/2009/05/19/caricature-ready-for-beta/" title="Caricature ready for beta"></a>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 &#8230;<p class="read-more"><a href="http://flanders.co.nz/2009/05/19/caricature-ready-for-beta/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://flanders.co.nz/2009/05/19/caricature-ready-for-beta/" title="Caricature ready for beta"></a><p>The last couple of days I’ve been getting <a href="http://github.com/casualjim/caricature" target="_blank">Caricature</a> 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.</p>
<p>   <span id="more-349"></span>
<p>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 <img src='http://flanders.co.nz/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> . </p>
<p>As for the latest changes:</p>
<p>Adding <em><strong>require ‘caricature’</strong></em> 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:<em><strong> require ‘caricature/clr’</strong></em>. I’ve done it this way so that I can still add Java and MacRuby support later on.</p>
<p>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, <strong><em>when_class_receives</em></strong> and<strong><em> did_class_receive?</em></strong>&#160; These methods enable you to setup substitutions for class method calls.</p>
<p>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.</p>
<p>Basically you download the ironruby release from <a href="http://ironruby.codeplex.com/Release/ProjectReleases.aspx" target="_blank">Codeplex</a></p>
<p>Then you make sure the path to ir.exe is in your PATH variable</p>
<p>After that you should be able to do igem list and get an empty result back.</p>
<p>now you need to install bacon you can do that by executing</p>
<p>igem install bacon</p>
<p>next you need the caricature gem</p>
<p>igem install caricature</p>
<p>You will also need rake to be installed to build the cs files included in the sources.</p>
<p>igem install rake</p>
<p>ok now you should be good to to</p>
<pre class="ruby" name="code">

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)
  end

  it "should work without expectations" do
    result = @dagger.attack @soldier
    result.should.equal nil
    @soldier.did_receive?(:survive_attack_with).with(@dagger).should.be.successful
  end

  it "should work for expectations with an argument constraint" do
    @soldier.when_receiving(:survive_attack_with).with(@dagger).return(5)
    @dagger.attack(@soldier).should.equal 5
    @soldier.did_receive?(:survive_attack_with).with(:any).should.be.successful
  end

end
</pre>
<p>you can get the gem from <a href="http://rubyforge.org/projects/caricature/" target="_blank">rubyforge</a>&#160;</p>
<p>Or from my github account: <a title="http://github.com/casualjim/caricature" href="http://github.com/casualjim/caricature">http://github.com/casualjim/caricature</a></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:5c7b90c5-1c5c-40e2-8aa3-b5a6fa638c0a" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/caricature" rel="tag">caricature</a>,<a href="http://technorati.com/tags/ironruby" rel="tag">ironruby</a>,<a href="http://technorati.com/tags/mocking" rel="tag">mocking</a></div>
]]></content:encoded>
			<wfw:commentRss>http://flanders.co.nz/2009/05/19/caricature-ready-for-beta/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mocking for IronRuby</title>
		<link>http://flanders.co.nz/2009/05/03/mocking-for-ironruby/</link>
		<comments>http://flanders.co.nz/2009/05/03/mocking-for-ironruby/#comments</comments>
		<pubDate>Sun, 03 May 2009 13:10:04 +0000</pubDate>
		<dc:creator>Ivan Porto Carrero</dc:creator>
				<category><![CDATA[IronRuby]]></category>
		<category><![CDATA[Mocking]]></category>

		<guid isPermaLink="false">http://flanders.co.nz/2009/05/03/mocking-for-ironruby/</guid>
		<description><![CDATA[<a href="http://flanders.co.nz/2009/05/03/mocking-for-ironruby/" title="Mocking for IronRuby"></a>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 &#8230;<p class="read-more"><a href="http://flanders.co.nz/2009/05/03/mocking-for-ironruby/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://flanders.co.nz/2009/05/03/mocking-for-ironruby/" title="Mocking for IronRuby"></a><p>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.&#160; 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.</p>
<p>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?</p>
<p> <span id="more-330"></span>
<p>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&#160; is no way in hell I will ever look at it again.</p>
<p>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.</p>
<p>1. Generate a proxy for an object so you can intercept method calls   <br />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.</p>
<p>So I’m starting to implement a way to generate proxies today.. This should be a piece of cake with Ruby <img src='http://flanders.co.nz/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Then I’ll look at implementing some constraints and verifications.</p>
<p>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.</p>
<p>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.&#160;&#160; My vote goes to the first option.</p>
<p>So a new project is born it’s called caricature and lives on github.&#160; Which will try to implement the ideas set forward in this blog post for <a href="http://ironruby.net" target="_blank">IronRuby</a>.</p>
<p><a href="http://github.com/casualjim/caricature" target="_blank">http://github.com/casualjim/caricature</a></p>
<div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:b9dc8a2b-10de-4666-ab6a-b6f91830119d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">Technorati Tags: <a href="http://technorati.com/tags/IronRuby" rel="tag">IronRuby</a>,<a href="http://technorati.com/tags/Mocking" rel="tag">Mocking</a></div>
]]></content:encoded>
			<wfw:commentRss>http://flanders.co.nz/2009/05/03/mocking-for-ironruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

