I'm getting an exception when I execute unit tests on a Nancy project. The application runs as expected, I only get the problem when running the unit tests.
I'm using Nancy 0.23.2.0. I'm also using Nancy.Authentication.Basic 0.23.2.0, Nancy.Bootstrappers.Unity 0.23.2.0 and Nancy.Testing 0.23.2.0 on the unit test project.
I got Nancy src for this version and could trace the problem to AppDomainAssemblyTypeScanner.UpdateTypes() specifically the linq expression.
public static void UpdateTypes()
{
UpdateAssemblies();
types = (from assembly in assemblies
from type in assembly.SafeGetExportedTypes()
where !type.IsAbstract
select type).ToArray();
}
The code for the test where I get the exception is this:
[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);
}
}
The module I'm trying to test:
public class IndexModule : NancyModule
{
public IndexModule()
{
this.RequiresAuthentication();
Get["/"] = parameters => View["index"];
}
}
The application Bootstrapper looks like this:
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);
}
}
The exception:
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()
The type ApplicationRegistrations
was renamed to Registrations
in commit 21c2f00
(v0.23) because it was changed to also include request-scoped registrations in addition to application-scoped registrations.
Sounds like you have a stale version of an assembly lying around somewhere, or an old NuGet dependency version. Try cleaning out your bin/obj folders and make sure all NuGet dependencies are updated.