James The Evangelist
James The Evangelist

Reputation: 144

Shim/fake object using external application

i've got class as below and i need to test RunMethod. Problem is, that ExternalClass needs some application run on server side (app providing external dll's). My dev environment has that app but my dev environment for test hasn't. My question is, how to shim/fake that ExternalClass in UnitTests to not check if app exist (test always faild in env without that external app)? This class is not important in tests but run automatically if i execute RunMethod.

public class MyExampleClass : ISomeInterface
{
    private static ExternalClass = new ExternalClass(string someParam);

    public object RunMethod()
    {
        /* Actuall code hear, doesn't matter */
        /* few unimportant (from the view point of tester) operation in ExternalClass (object loggin etc.) */
        return someVar;
    }
}

Upvotes: 1

Views: 701

Answers (1)

Old Fox
Old Fox

Reputation: 8725

MsFakes generate a property named AllInstances to the shim class, through this property you can override the behavior of any instance method:

[TestMethod]
public void TestMethod1()
{
    using (ShimsContext.Create())
    {
        ShimExternalClass.AllInstances.ToString01 = () =>
        {
            return String.Empty();
        };

        Assert.IsNull(new ExternalClass().ToString());
    }
}

Upvotes: 2

Related Questions