我正在尝试编写集成测试。因此,我需要在单元测试的预启动初始化阶段执行WebActivator的启动方法。
我试过了
[TestClass]
public class UnitTests
{
[ClassInitialize]
public static void Init(TestContext c)
{
WebActivator.ActivationManager.Run();
}但它总是会给我错误信息:
Unable to create instance of class EL.NET.SecurityAdapter.Tests.UnitTests. Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: This method can only be called during the application's pre-start initialization stage..使用像这样的堆栈跟踪
System.Web.Compilation.BuildManager.ThrowIfPreAppStartNotRunning()
Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.LegacyModuleRegistrar.RegisterModule(Type moduleType)
Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(Type moduleType)
<myproject>.Tests.App_Start.NinjectWebCommon.Start() in <myprojectPath>Tests\App_Start\NinjectWebCommon.cs: line 25
System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
WebActivator.BaseActivationMethodAttribute.InvokeMethod()
WebActivator.ActivationManager.RunActivationMethods[T]()
WebActivator.ActivationManager.Run()
<myproject>.Tests.UnitTests..ctor() in <myprojectPath>Tests\UnitTests.cs: line 40我知道单元测试应该与mock一起工作,但我真的需要那些集成测试。
发布于 2012-06-22 15:07:08
我想运行WebActivator来创建我的Ninject映射。
我最终为DI容器创建了一个包装器接口。我的存储库只能通过这个包装器访问DI。
在web应用程序中,我使用了该接口的一个特定于Ninject的实现,并且为了测试,我做了一个自定义的实现。
我仍然认为应该可以以某种方式在单元测试中使Ninject工作,但现在这也是可行的。
https://stackoverflow.com/questions/11098390
复制相似问题