# Mocking behavior between clr types

**URL:** <https://www.ruby-forum.com/t/mocking-behavior-between-clr-types/182190>\
**Category:** IronRuby\
**Created:** [January 24, 2010, 7:26pm UTC](https://www.ruby-forum.com/t/mocking-behavior-between-clr-types/182190 "2010-01-24T19:26:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew\_Wilson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@andrew\_Wilson](https://www.ruby-forum.com/u/andrew_Wilson)\
**Post date:** [January 24, 2010, 7:26pm UTC](https://www.ruby-forum.com/t/mocking-behavior-between-clr-types/182190/1 "2010-01-24T19:26:08Z")

</div>

As I have been working more and more with testing C# via IronRuby &  
Rspec, I  
have found myself wanting to use more and more of the rspec framework  
for  
testing. I have also found problems that I am not sure how to work  
through  
yet.

For instance lets say I have two c# classes:

public class Foo  
{  
private Bar \_bar;

public Foo(Bar someBar)  
{  
this.\_bar = someBar;  
}

public int GetBarCount()  
{  
return this.\_bar.Count;  
}

}

public class Bar  
{  
private int \_count = 0;

public Count  
{  
get { return \_count;}  
}  
}

in my spec test I want to do something like this:

b = Bar.new  
b.stub!(:Count).and\_return(400)

f = Foo.new(b)  
f.GetBarCount(),should == 100

The problem is that because I am stubbing the Bar instance via the dlr,  
the  
CLR call on f never sees the stub and just returns 0.

Is there any way to make this work? I was wondering if I could maybe  
make  
my Foo instance a proxy or mock itself and see if that would let the  
method  
calls work… but I haven’t had any luck ☹

-A

---

<div class="post-metadata">

**Author:** ![andrew\_Wilson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@andrew\_Wilson](https://www.ruby-forum.com/u/andrew_Wilson)\
**Post date:** [January 24, 2010, 7:42pm UTC](https://www.ruby-forum.com/t/mocking-behavior-between-clr-types/182190/2 "2010-01-24T19:42:31Z")

</div>

you can use caricature to solve that problem

gem install caricature

for rspec you do

require ‘caricature’

Spec::Runner.configure do |config|  
config.mock\_with Caricature::RSpecAdapter  
config.include Caricature::RSpecMatchers  
end

But it works slightly different as it won’t verify unless you tell it to  
verify.  
it has no mock or stub knowledge but works from the assumption you want  
to  
stub unless you add a verify call.

## the bacon integration specs show off all the combinations.

Met vriendelijke groeten - Best regards - Salutations  
Ivan Porto C.  
Blog: [http://flanders.co.nz](http://flanders.co.nz)  
Twitter: [http://twitter.com/casualjim](http://twitter.com/casualjim)  
Author of IronRuby in Action ([IronRuby in Action - Ivan Porto Carrero and Adam Burmister | Manning](http://manning.com/carrero))
