Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Microsoft Fakes to Shim a method with ref parameters

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!

like image 466
Pradeep Balakrishnan Avatar asked Aug 24 '12 18:08

Pradeep Balakrishnan


People also ask

What is Microsoft Fakes?

Microsoft Fakes helps you isolate the code you're testing by replacing other parts of the application with stubs or shims. The stubs and shims are small pieces of code that are under the control of your tests.

What is a shim C#?

Shim, in C#, is a template class that is derived from a base class with derived classes that inherit the data and behavior of the base class and vary only in the type. The derived class of the shim class reuses the implementation provided by the shim class.

What does add fakes Assembly do?

When you click on “Add Fakes Assembly” it will create the same assembly again with the Fakes keyword added that we will use for our testing purposes. The Fakes framework uses delegate-based methods for writing code.

What is Shim and stub?

Shims are generally used to provide mocks from assemblies outside of your solution, whereas stubs are used to create mocks of classes within your solution.


1 Answers

using (ShimsContext.Create())
{
    ShimBranding.GetBrandingInt32Int32StringStringRef =
        (int providerId, int employerId, string brandingElement, ref string brandingValue) =>
        {
            brandingValue = "Blah";
            return true;
        };
}
like image 60
Oleg Sych Avatar answered Oct 08 '22 03:10

Oleg Sych