首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从单元测试启动WebActivator

从单元测试启动WebActivator
EN

Stack Overflow用户
提问于 2012-06-19 17:42:59
回答 1查看 697关注 0票数 1

我正在尝试编写集成测试。因此,我需要在单元测试的预启动初始化阶段执行WebActivator的启动方法。

我试过了

代码语言:javascript
复制
[TestClass]
public class UnitTests
{
    [ClassInitialize]
    public static void Init(TestContext c)
    {   
        WebActivator.ActivationManager.Run();
    }

但它总是会给我错误信息:

代码语言:javascript
复制
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..

使用像这样的堆栈跟踪

代码语言:javascript
复制
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一起工作,但我真的需要那些集成测试。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-22 15:07:08

我想运行WebActivator来创建我的Ninject映射。

我最终为DI容器创建了一个包装器接口。我的存储库只能通过这个包装器访问DI。

在web应用程序中,我使用了该接口的一个特定于Ninject的实现,并且为了测试,我做了一个自定义的实现。

我仍然认为应该可以以某种方式在单元测试中使Ninject工作,但现在这也是可行的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11098390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档