0
我試圖用NSubstitute嘲笑IComponentContext
如下:模擬Autofac.IComponentContext單元測試C#
[TestClass()]
public class SyncRepositoryFactoryTests
{
private IComponentContext _container;
private SyncRepositoryFactory _factory;
[TestInitialize]
public void Initialize()
{
_container = Substitute.For<IComponentContext>();
_factory = new SyncRepositoryFactory(_container);
}
[TestMethod]
public void Get_SyncRepositoryOfITestEntity_Success()
{
var repository = Substitute.For<IRepository<TestEntity>>();
_container.Resolve<IRepository<TestEntity>>().Returns(repository);
var result = _factory.Get<ITestEntity>();
Assert.IsNotNull(result);
Assert.IsTrue(result is ISyncRepository<ITestEntity>);
}
public interface ITestEntity
{
}
public class TestEntity : ITestEntity
{
}
}
,但我得到一個異常:
ComponentNotRegisteredException: The requested service 'Hvb.eMarketAdvisor.Repository.SharePoint.IRepository`1[[Hvb.eMarketAdvisor. Repository.SharePoint.Tests.Units.SyncRepositoryFactoryTests+TestEntity, Hvb.eMarketAdvisor.Repository.SharePoint.Tests.Units, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
當我嘗試創建一個模擬爲什麼IComponentContext
是否嘗試解決依賴關係?
嘲諷'IComponentContext'有什麼意義?你想在這裏測試什麼? –
我在這裏測試完全隔離工廠Get()方法的邏輯。但是,關於嘲諷IComponentContext用於其他任何目的,這個問題一般。 – Leszek
如何實現Get方法?如果它只是調用解決方案,那麼從測試中得不到任何有用的東西。 –