Moq with Autofac Func<Owned<IClass>>

Viewed 1317

I'm trying to swtich some code I have to use Owned Instances instead of passing around the container.

public class SyncManager
{
    private Func<Owned<ISynchProcessor>> _syncProcessor = null;

    public SyncManager(Func<Owned<ISynchProcessor>> syncProcessor)
    {
        _syncProcessor = syncProcessor;
    }

    private void Handle()
    {
        using (var service = _syncProcessor())
        {
            service.Value.Process();   
        }
    }
}

Now I want to Moq the Func< Owned> and inject it but the calls to _syncProcessor() don't resolve anything So I tried this.

Mock<ITimer> timer = new Mock<ITimer>();
Mock<Func<Owned<ISynchProcessor>>> syncProcessor = new Mock<Func<Owned<ISynchProcessor>>>();
Mock<Owned<ISynchProcessor>> proc = new Mock<Owned<ISynchProcessor>>();

[TestInitialize]
public void TestInitialize()
{
    timer = new Mock<ITimer>();
    syncProcessor = new Mock<Func<Owned<ISynchProcessor>>>();
    syncProcessor.Setup(item => item.Invoke()).Returns(() => proc.Object);
}

but that gives me a run time error that it can't cast object of type System.Linq.Expressions.InstanceMethodCallExpressionN to type System.Linq.InvocationExpression

1 Answers
Related