Using Microsoft Fakes to Shim a method with ref parameters

Pradeep Balakrishnan picture Pradeep Balakrishnan · Aug 24, 2012 · Viewed 7k times · Source

I have a static method with ref parameters in my legacy (untestable) dll. I am trying to write unit tests for a class that calls into this method.

public static class Branding
{
    ...
    ...

    static public bool GetBranding(Int32 providerId,
        Int32 employerId,
        string brandingElement,
        ref string brandingValue)

    ...
    ...
}

I need help writing a shim statement for this call

ShimBranding.GetBrandingInt32Int32StringStringRef = 
    ( providerId, employerId, element, { ====> WHAT GOES HERE <===== } )
    => 
    true;

Thanks!

Answer

Oleg Sych picture Oleg Sych · Oct 13, 2012
using (ShimsContext.Create())
{
    ShimBranding.GetBrandingInt32Int32StringStringRef =
        (int providerId, int employerId, string brandingElement, ref string brandingValue) =>
        {
            brandingValue = "Blah";
            return true;
        };
}