Nancy.Bootstrapper.AppDomainAssemblyTypeScanner'的类型初始值设定项。抛出一个例外

时间:2014-08-01 12:42:07

标签: c# .net unit-testing .net-assembly nancy

我在Nancy项目上执行单元测试时遇到异常。应用程序按预期运行,我只在运行单元测试时遇到问题。

我正在使用Nancy 0.23.2.0。我还在单元测试项目中使用Nancy.Authentication.Basic 0.23.2.0,Nancy.Bootstrappers.Unity 0.23.2.0和Nancy.Testing 0.23.2.0。

我为此版本获得了Nancy src,并且可以将问题跟踪到AppDomainAssemblyTypeScanner.UpdateTypes(),特别是linq表达式。

public static void UpdateTypes()
{
    UpdateAssemblies();

    types = (from assembly in assemblies
        from type in assembly.SafeGetExportedTypes()
        where !type.IsAbstract
        select type).ToArray();
}

我得到异常的测试代码是:

[TestFixture]
public class IndexModuleTests
{
    private Browser _browser;

    [SetUp]
    public void SetUp()
    {
        var bootstrapper = new ConfigurableBootstrapper(bootstrapperConfigurator =>
        {
            bootstrapperConfigurator.RootPathProvider(new TestRootPathProvider());
            bootstrapperConfigurator.Module<IndexModule>();
            bootstrapperConfigurator.RequestStartup((container, pipelines, context) =>
            {
                context.CurrentUser = new UserIdentity {UserName = "demo"};
            });
        });

        _browser = new Browser(bootstrapper);          
    }

    [Test]
    public void ShouldAllowAuthenticatedUsersToBrowseSecuredPage()
    {
        var result = _browser.Get("/index", with => with.HttpRequest());

        result.StatusCode.Should().Be(HttpStatusCode.OK);
    }
}

我试图测试的模块:

public class IndexModule : NancyModule
{
    public IndexModule()
    {
        this.RequiresAuthentication();

        Get["/"] = parameters => View["index"];
    }
}

Bootstrapper应用程序如下所示:

public class Bootstrapper : UnityNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(IUnityContainer contrainer)
    {
       contrainer.RegisterType<IUserValidator, UserValidator>();
    }

    protected override void ApplicationStartup(IUnityContainer container, IPipelines pipelines)
    {
        base.ApplicationStartup(container, pipelines);

        pipelines.EnableBasicAuthentication(new BasicAuthenticationConfiguration(
            container.Resolve<IUserValidator>(),
            "MyAppDomain"));
    }

    protected override void ConfigureConventions(NancyConventions nancyConventions)
    {
        nancyConventions
            .StaticContentsConventions
            .Add(StaticContentConventionBuilder.AddDirectory("js", @"js"));

        nancyConventions
            .StaticContentsConventions
            .Add(StaticContentConventionBuilder.AddDirectory("css", @"css"));

        nancyConventions
            .StaticContentsConventions
            .Add(StaticContentConventionBuilder.AddDirectory("img", @"img"));

        nancyConventions
            .StaticContentsConventions
            .Add(StaticContentConventionBuilder.AddDirectory("fonts", @"fonts"));

        base.ConfigureConventions(nancyConventions);
    }
}

例外:

SetUp : System.TypeInitializationException : The type initializer for 'Nancy.Bootstrapper.AppDomainAssemblyTypeScanner' threw an exception.
----> System.TypeLoadException : Could not load type 'Nancy.Bootstrapper.ApplicationRegistrations' from assembly 'Nancy, Version=0.23.2.0, Culture=neutral, PublicKeyToken=null'.
at Nancy.Bootstrapper.AppDomainAssemblyTypeScanner.TypesOf(ScanMode mode)
at Nancy.Conventions.NancyConventions.BuildDefaultConventions()
at Nancy.Conventions.NancyConventions..ctor()
at Nancy.Bootstrapper.NancyBootstrapperBase`1..ctor()
at Nancy.Bootstrapper.NancyBootstrapperWithRequestContainerBase`1..ctor()
at Nancy.Testing.ConfigurableBootstrapper..ctor(Action`1 configuration)
at Pandora.Web.Backoffice.Lite.Tests.IndexModuleTests.SetUp() in IndexModuleTests.cs: line 17
--TypeLoadException
at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
at System.Reflection.RuntimeAssembly.GetExportedTypes()
at Nancy.Extensions.AssemblyExtensions.SafeGetExportedTypes(Assembly assembly)
at Nancy.Bootstrapper.AppDomainAssemblyTypeScanner.<UpdateTypes>b__16(Assembly assembly)
at System.Linq.Enumerable.<SelectManyIterator>d__31`3.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray(IEnumerable`1 source)
at Nancy.Bootstrapper.AppDomainAssemblyTypeScanner.UpdateTypes()
at Nancy.Bootstrapper.AppDomainAssemblyTypeScanner.LoadAssembliesWithNancyReferences()
at Nancy.Bootstrapper.AppDomainAssemblyTypeScanner..cctor()

1 个答案:

答案 0 :(得分:4)

类型ApplicationRegistrations已在commit 21c2f00(v0.23)中重命名为Registrations,因为除了应用程序范围的注册之外,它还更改为包括请求范围的注册。

听起来你有一个陈旧版本的程序集,或者旧的NuGet依赖版本。尝试清理bin / obj文件夹并确保更新所有NuGet依赖项。