使用AutoSubstitute
库中的AutofacContrib.NSubstitute
,我想将AutoSubstitute中的容器实例传递给由AutoSubstitute解析的类。尽管尽管IContainer
是通过AutoSubstitute.Provide()
方法设置的,但并不是希望通过构造函数将IContainer实例传递给SomeAppClass
的方法。
using System;
using Autofac;
using AutofacContrib.NSubstitute;
namespace NSubstituteTest
{
public class Program
{
public static void Main(string[] args)
{
var autoSubstitute = new AutoSubstitute();
autoSubstitute.Provide<IContainer>(autoSubstitute.Container);
autoSubstitute.Resolve<SomeAppClass>();
Console.Read();
}
}
public class SomeAppClass
{
private readonly IContainer _container;
private readonly ITestClass _testClass;
// The IContainer instance should be the autoSubstitute.Container set on line 13 using the Provide() method
public SomeAppClass(IContainer container)
{
_container = container;
// Error thrown when trying to retrieve an automocked instance for ITestClass
_testClass = _container.Resolve<ITestClass>();
}
}
public interface ITestClass
{
void TestMethod();
}
}