我在解决方案中使用了Autofac和Automapper。我写了单元测试,它可以在本地运行而没有任何问题。当我创建请求请求时,单元测试在RELEASE中成功运行;但是,在DEBUG中,它会无限期运行,并且经过一段时间(即45分钟)后,它只会失败。
我尝试调整代码,使其仅使用Autofac的container
而不使用scope
来解决某些依赖性。我正在尝试测试应用程序的分辨率以及映射器配置文件的AssertConfigurationIsValid
;但问题仍然存在。
public static IContainer Configure()
{
var builder = new ContainerBuilder();
//Application
builder.RegisterType<DerivedBatchVolumesApplication>().AsSelf();
//Features
builder.RegisterType<QEndOfMonthCalculator>().As<IQEndOfMonthCalculator>();
builder.RegisterType<QProgressiveRounder>().As<IQProgressiveRounder>();
//Configuration
builder.Register(x => QEndOfMonthCalculatorConfiguration.FromMetadata()).As<IQEndOfMonthCalculatorConfiguration>();
builder.Register(x => QDerivedBatchVolumesConfiguration.FromMetadata()).As<IQDerivedBatchVolumesConfiguration>();
//Data
builder.RegisterType<QTranTicketRepository>().As<IQRepository<TranTicketDO>>();
builder.RegisterType<QCtBatchRepository>().As<IQRepository<CtBatchDO>>();
builder.RegisterType<QPaystationRepository>().As<IQRepository<PaystationDO>>();
builder.RegisterType<QTicketVolRepository>().As<IQRepository<TicketVolDO>>();
builder.RegisterType<QSctrlRelatedCtrRepository>().As<IQRepository<SctrlRelatedCtrDO>>();
builder.RegisterType<QAllocVolRepository>().As<IQRepository<AllocVolDO>>();
//Mapper
builder.RegisterType<QMapper>().As<IQMapper>();
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.Where(x => x.GetCustomAttribute<InjectableAttribute>() != null)
.AsImplementedInterfaces();
//Data Access
builder.RegisterType<TranTicket>().AsSelf();
builder.RegisterType<CtBatch>().AsSelf();
builder.RegisterType<Paystation>().AsSelf();
builder.RegisterType<TicketVol>().AsSelf();
builder.RegisterType<SctrlRelatedCtr>().AsSelf();
builder.RegisterType<AllocVol>().AsSelf();
return builder.Build();
}
[TestInitialize]
public void Setup()
{
var container = ContainerConfiguration.Configure();
_scope = container.BeginLifetimeScope();
_mapper = _scope.Resolve<IQMapper>();
}
[TestCleanup]
public void Cleanup()
{
_scope.Dispose();
}
[TestMethod]
public void MapperConfigurations_AreValid()
{
_mapper.AssertConfigurationIsValid();
}
[TestInitialize]
public void Setup()
{
_endOfMonthCalculator = new Mock<IQEndOfMonthCalculator>();
_tranTicket = new Mock<IQRepository<TranTicketDO>>();
_ctBatch = new Mock<IQRepository<CtBatchDO>>();
_paystation = new Mock<IQRepository<PaystationDO>>();
_ticketVol = new Mock<IQRepository<TicketVolDO>>();
_sctrlRelatedCtr = new Mock<IQRepository<SctrlRelatedCtrDO>>();
_allocVol = new Mock<IQRepository<AllocVolDO>>();
_derBatchVolConfiguration = new Mock<IQDerivedBatchVolumesConfiguration>();
_container = ContainerConfiguration.Configure();
_progressiveRounder = _container.Resolve<IQProgressiveRounder>();
_mapper = _container.Resolve<IQMapper>();
}
[TestCleanup]
public void Cleanup()
{
_container.Dispose();
}
[TestMethod]
public void DerivedBatchVolumesApplication_Resolves()
{
//Arrange & Act
var application = _container.Resolve<DerivedBatchVolumesApplication>();
//Assert
application.ShouldNotBeNull();
application.ShouldBeOfType<DerivedBatchVolumesApplication>();
}
我希望单元测试能够像调试RELEASE一样成功地运行于DEBUG。