Moq with Autofac Func <Owner <IClass>>

I am trying to use swtich code, which I should use Owned Instances instead of going 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 Moq the Func <Own> and enter it, but calls to _syncProcessor () do not solve 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 it gives me a runtime error that it cannot use an object like System.Linq.Expressions.InstanceMethodCallExpressionN to enter System.Linq.InvocationExpression

+7
source share
1 answer

Ok, I think I joked a bit about this. There is no reason to mock Func. Doing this seems to get me back on track.

  Func<Owned<ISynchProcessor>> syncProcessor; Mock<ISynchProcessor> proc = new Mock<ISynchProcessor>(); [TestInitialize] public void TestInitialize() { syncProcessor = () => new Owned<ISynchProcessor>(proc.Object, new Mock<IDisposable>().Object); } 

Now I can just pass the specific Func to my classes and return to it the class that I really wanted to execute Mock, my processor and verify that it is calling, running and deleting.

+10
source

All Articles