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
Ok I think I got a little Mock happy on this. There's no reason to be mocking out the Func. Doing this seems to have 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 concrete Func into my classes and have it return me the class that I really wanted to Mock, my processor, and validate that it's calls are being done and disposed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With