Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Rhino Mocks and mocking Out/Ref params

I thought I would share a little nugget of greatness with RhinoMocks

Today I was in the process of mocking out a webservice call for an application I was working on.  But the method I wanted to mock took a out parameter in its signature.  Normally this is not an issue, but since this out parameter result was used in my code, I needed to setup an expectation on that value.

Fortunately for us, Rhino handles this pretty easily.  In order to set expectations on an Our or Ref all I need to do is add a call to the OutRef() method on my Expect call.

Here is an example of this:

using ( mocker.Record() )
{
    Expect.Call( mockAPI.Create( out emptyString, out emptyString ) ).IgnoreArguments().OutRef( "", "OK" ).Repeat.Once().Return( results );
}

If you notice when I call OutRef, I am providing 2 values.  The first is an empty string because I do not care about that value and the second is "OK".  The "OK" value is the value that I cared about. 

The OutRef method takes in one parameter, a Parameter Array.  In order to use this all you need to do is pass in a value for each out/ref argument in there ordinal positions.

Hope this helps someone.

Till next time,



Comments

Reflective Perspective - Chris Alcock » The Morning Brew #75 said:

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #75

# April 17, 2008 3:30 AM

James Shumaker said:

Derik,

Thank you for taking the time to post this, it was exactly what I needed.

Thanks!

# July 3, 2008 9:27 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Red-Gate!