Mocking Autofac's "Resolve" extension method with TypeMock

Posted by Lockshopr on Stack Overflow See other posts from Stack Overflow or by Lockshopr
Published on 2010-05-01T13:53:11Z Indexed on 2010/05/01 13:57 UTC
Read the original article Hit count: 326

I'm trying to mock an Autofac resolve, such as:

using System;
using Autofac;
using TypeMock.ArrangeActAssert;

class Program
{
    static void Main(string[] args)
    {
        var inst = Isolate.Fake.Instance<IContainer>();
        Isolate.Fake.StaticMethods(typeof(ResolutionExtensions), Members.ReturnNulls);
        Isolate.WhenCalled(() => inst.Resolve<IRubber>()).WillReturn(new BubbleGum());
        Console.Out.WriteLine(inst.Resolve<IRubber>());
    }
}

public interface IRubber
{}

public class BubbleGum : IRubber
{}

Coming from Moq, the syntax and exceptions from TypeMock confuse me a great deal. Having initially run this in a TestMethod, I kept getting an exception resembling "WhenCalled cannot be run without a complementing behavior". I tried defining behaviors for everyone and their mothers, but to no avail.

Then I debug stepped through the test run, and saw that an actual exception was fired from Autofac: IRubber has not been registered.

So it's obvious that the static Resolve function isn't being faked, and I can't get it to be faked, no matter how I go about hooking it up.

Isolate.WhenCalled(() => ResolutionExtensions.Resolve<IRubber>(null)).WillReturn(new BubbleGum());

... throws an exception from Autofac complaining that the IComponentContext cannot be null. Feeding it the presumably faked IContainer (or faking an IComponentContext instead) gets me back to the "IRubber not registered" error.

© Stack Overflow or respective owner

Related posts about typemock

Related posts about mocking