ASP.NET系列:单元测试之StructureMap

王朝学院·作者佚名  2016-08-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

asp.net使用StructureMap等依赖注入组件时最重要就是EntityFramework的DbContext对象要保证在每次HttPRequest只有一个DbContext实例,这里将使用第三方提供的HttpSimulator进行测试。

1.定义IDependency接口创建屏蔽不同依赖注入组件使用差别的接口。

publicinterfaceIDependency

{voidBuild();voidEndRequest();voidAddTransient(Typefrom, Type to,objectinstance =null);voidAddScoped(Typefrom, Type to,objectinstance =null);voidAddSingleton(Typefrom, Type to,objectinstance =null);objectGetInstance(Type type);

IEnumerable GetAllInstances(Type type);

}

2.提供StructureMap的适配类StructureMapAdapterpublicclassStructureMapAdapter : IDependency, IDisposable

{privatebool_disposed =false;privateContainer _container;privateRegistry _registry;publicStructureMapAdapter()

{this._registry =newRegistry();

}publicvoidBuild()

{

_container=newContainer(_registry);

}publicvoidEndRequest()

{

HttpContextLifecycle.DisposeAndClearAll();

}publicvoidAddTransient(Typefrom, Type to,objectinstance =null)

{if(instance ==null)

{

_registry.For(from).Use(to).LifecycleIs<TransientLifecycle>();

}else{

_registry.For(from).Use(instance).LifecycleIs<TransientLifecycle>();

}

}publicvoidAddScoped(Typefrom, Type to,objectinstance =null)

{if(instance ==null)

{

_registry.For(from).Use(to).LifecycleIs<HttpContextLifecycle>();

}else{

_registry.For(from).Use(instance).LifecycleIs<HttpContextLifecycle>();

}

}publicvoidAddSingleton(Typefrom, Type to,objectinstance =null)

{if(instance ==null)

{

_registry.For(from).Use(to).LifecycleIs<SingletonLifecycle>();

}else{

_registry.For(from).Use(instance).LifecycleIs<SingletonLifecycle>();

}

}publicobjectGetInstance(Type type)

{return_container.GetInstance(type);

}publicIEnumerable GetAllInstances(Type type)

{return_container.GetAllInstances(type);

}publicvoidDispose()

{

Dispose(true);

GC.SuppressFinalize(this);

}protectedvirtualvoidDispose(booldisposing)

{if(!_disposed)

{if(disposing)

{this._container.Dispose();

}

_disposed=true;

}

}

}

3.使用HttpSimulator进行单元测试publicclassStructureMapAdapterTest

{

[Fact]publicvoidTransientTest()

{

IDependency dependency=newStructureMapAdapter();

dependency.AddTransient(typeof(ITest),typeof(Test));

dependency.Build();varversion1 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;varversion2 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;

Assert.NotEqual(version1, version2);

}

[Fact]publicvoidSingletonTest()

{

IDependency dependency=newStructureMapAdapter();

dependency.AddSingleton(typeof(ITest),typeof(Test));

dependency.Build();varversion1 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;varversion2 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;

Assert.Equal(version1, version2);

}

[Fact]publicvoidScopedTest()

{varversion1 ="";varversion2 ="";using(HttpSimulator simulator =newHttpSimulator())

{

IDependency dependency=newStructureMapAdapter();

dependency.AddScoped(typeof(ITest),typeof(Test));

dependency.Build();

simulator.SimulateRequest(newUri("http://localhost/"));

version1= ((ITest)dependency.GetInstance(typeof(ITest))).Version;

version2= ((ITest)dependency.GetInstance(typeof(ITest))).Version;

Assert.Equal(version1, version2);

}using(HttpSimulator simulator =newHttpSimulator())

{

IDependency dependency=newStructureMapAdapter();

dependency.AddScoped(typeof(ITest),typeof(Test));

dependency.Build();

simulator.SimulateRequest(newUri("http://localhost/"));

version1= ((ITest)dependency.GetInstance(typeof(ITest))).Version;

}using(HttpSimulator simulator =newHttpSimulator())

{

IDependency dependency=newStructureMapAdapter();

dependency.AddScoped(typeof(ITest),typeof(Test));

dependency.Build();

simulator.SimulateRequest(newUri("http://localhost/"));

version2= ((ITest)dependency.GetInstance(typeof(ITest))).Version;

}

Assert.NotEqual(version1, version2);

}

}publicinterfaceITest

{

String Version {get; }

}publicclassTest : ITest

{privatestring_version =Guid.NewGuid().ToString();publicstringVersion {get{returnthis._version; } }

}

运行结果:

[flash=500,400]http://s1.knowsky.com/20160211/x1nbcdqvtam15.png[/flash]

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航