Permalink
Browse files

Updated GetModule implementation and added more tests

  • Loading branch information...
1 parent 0feb6b9 commit 98fc4086f32fbaeec50c1e41400c00cdc2eed6af @thecodejunkie thecodejunkie committed Feb 23, 2013
2 dependencies/Nancy
@@ -1 +1 @@
-Subproject commit 76749670627b54154aa64321c25cdf083d18c791
+Subproject commit 7126711f7306dfcfdf5720c24b962aff94cce1ec
View
56 src/Nancy.Bootstrappers.Ninject.Tests/FakeNancyModuleWithBasePath.cs
@@ -0,0 +1,56 @@
+namespace Nancy.Bootstrappers.Ninject.Tests
+{
+ using System;
+
+ public class FakeNancyModuleWithBasePath : NancyModule
+ {
+ public FakeNancyModuleWithBasePath()
+ : base("/fake")
+ {
+ Delete["/"] = x =>
+ {
+ throw new NotImplementedException();
+ };
+
+ Get["/route/with/some/parts"] = x =>
+ {
+ return "FakeNancyModuleWithBasePath";
+ };
+
+ Get["/should/have/conflicting/route/defined"] = x =>
+ {
+ return "FakeNancyModuleWithBasePath";
+ };
+
+ Get["/child/{value}"] = x =>
+ {
+ throw new NotImplementedException();
+ };
+
+ Get["/child/route/{value}"] = x =>
+ {
+ return "test";
+ };
+
+ Get["/"] = x =>
+ {
+ throw new NotImplementedException();
+ };
+
+ Get["/foo/{value}/bar/{capture}"] = x =>
+ {
+ return string.Concat(x.value, " ", x.capture);
+ };
+
+ Post["/"] = x =>
+ {
+ return "Action result";
+ };
+
+ Put["/"] = x =>
+ {
+ throw new NotImplementedException();
+ };
+ }
+ }
+}
View
44 src/Nancy.Bootstrappers.Ninject.Tests/FakeNancyModuleWithDependency.cs
@@ -0,0 +1,44 @@
+namespace Nancy.Bootstrappers.Ninject.Tests
+{
+ public class FakeNancyModuleWithDependency : NancyModule
+ {
+ public IDependency Dependency { get; set; }
+
+ public IFoo FooDependency { get; set; }
+
+ /// <summary>
+ /// Initializes a new instance of the FakeNancyModuleWithDependency class.
+ /// </summary>
+ public FakeNancyModuleWithDependency(IDependency dependency, IFoo foo)
+ {
+ Dependency = dependency;
+ FooDependency = foo;
+ }
+ }
+
+ public interface IFoo
+ {
+ }
+
+ public class Foo : IFoo
+ {
+ }
+
+ public interface IDependency
+ {
+ IFoo FooDependency { get; set; }
+ }
+
+ public class Dependency : IDependency
+ {
+ public IFoo FooDependency { get; set; }
+
+ /// <summary>
+ /// Initializes a new instance of the Dependency class.
+ /// </summary>
+ public Dependency(IFoo fooDependency)
+ {
+ FooDependency = fooDependency;
+ }
+ }
+}
View
33 src/Nancy.Bootstrappers.Ninject.Tests/FakeNinjectNancyBootstrapper.cs
@@ -1,12 +1,15 @@
-namespace Nancy.Bootstrappers.Ninject.Tests
+namespace Nancy.Bootstrappers.Ninject.Tests
{
using Bootstrapper;
+ using global::Ninject;
- /// <summary>
- /// Fake Ninject boostrapper that can be used for testing.
- /// </summary>
- public class FakeNinjectNancyBootstrapper : NinjectNancyBootstrapper
+ /// <summary>
+ /// Fake Ninject boostrapper that can be used for testing.
+ /// </summary>
+ public class FakeNinjectNancyBootstrapper : NinjectNancyBootstrapper
{
+ public bool RequestContainerConfigured { get; set; }
+ public bool ApplicationContainerConfigured { get; set; }
private readonly NancyInternalConfiguration configuration;
public FakeNinjectNancyBootstrapper()
@@ -22,7 +25,27 @@ public FakeNinjectNancyBootstrapper(NancyInternalConfiguration configuration)
protected override NancyInternalConfiguration InternalConfiguration
{
get { return configuration ?? base.InternalConfiguration; }
+ }
+
+ protected override void ConfigureApplicationContainer(IKernel existingContainer)
+ {
+ base.ConfigureApplicationContainer(existingContainer);
+ this.ApplicationContainerConfigured = true;
+ }
+
+ protected override void ConfigureRequestContainer(IKernel container, NancyContext context)
+ {
+ base.ConfigureRequestContainer(container, context);
+
+ container.Bind(typeof(IFoo)).To(typeof(Foo)).InSingletonScope();
+ container.Bind(typeof(IDependency)).To(typeof(Dependency)).InSingletonScope();
+ this.RequestContainerConfigured = true;
+ }
+
+ public T Resolve<T>()
+ {
+ return this.ApplicationContainer.Get<T>();
}
}
}
View
2 src/Nancy.Bootstrappers.Ninject.Tests/Nancy.Bootstrappers.Ninject.Tests.csproj
@@ -59,6 +59,8 @@
<Compile Include="..\..\dependencies\Nancy\src\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
+ <Compile Include="FakeNancyModuleWithBasePath.cs" />
+ <Compile Include="FakeNancyModuleWithDependency.cs" />
<Compile Include="FakeNinjectNancyBootstrapper.cs" />
<Compile Include="NinjectBootstrapperBaseFixture.cs" />
<Compile Include="NinjectNancyBootstrapperFixture.cs" />
View
94 src/Nancy.Bootstrappers.Ninject.Tests/NinjectNancyBootstrapperFixture.cs
@@ -1,6 +1,8 @@
namespace Nancy.Bootstrappers.Ninject.Tests
-{
- using Nancy.Tests;
+{
+ using System.Linq;
+ using Nancy.Tests;
+ using Routing;
using Xunit;
public class NinjectNancyBootstrapperFixture
@@ -22,6 +24,94 @@ public void Should_be_able_to_resolve_engine()
// Then
engine.ShouldNotBeNull();
+ }
+
+ [Fact]
+ public void GetAllModules_Returns_As_MultiInstance()
+ {
+ // Given
+ this.bootstrapper.GetEngine();
+ var context = new NancyContext();
+
+ // When
+ var output1 = this.bootstrapper.GetAllModules(context).FirstOrDefault(nm => nm.GetType() == typeof(FakeNancyModuleWithBasePath));
+ var output2 = this.bootstrapper.GetAllModules(context).FirstOrDefault(nm => nm.GetType() == typeof(FakeNancyModuleWithBasePath));
+
+ // Then
+ output1.ShouldNotBeNull();
+ output2.ShouldNotBeNull();
+ output1.ShouldNotBeSameAs(output2);
+ }
+
+ [Fact]
+ public void GetAllModules_Configures_Child_Container()
+ {
+ // Given
+ this.bootstrapper.GetEngine();
+ this.bootstrapper.RequestContainerConfigured = false;
+
+ // When
+ this.bootstrapper.GetAllModules(new NancyContext());
+
+ // Then
+ this.bootstrapper.RequestContainerConfigured.ShouldBeTrue();
+ }
+
+ [Fact]
+ public void GetModule_Configures_Child_Container()
+ {
+ // Given
+ this.bootstrapper.GetEngine();
+ this.bootstrapper.RequestContainerConfigured = false;
+
+ // When
+ this.bootstrapper.GetModule(typeof(FakeNancyModuleWithBasePath), new NancyContext());
+
+ // Then
+ this.bootstrapper.RequestContainerConfigured.ShouldBeTrue();
+ }
+
+ [Fact]
+ public void GetEngine_ConfigureApplicationContainer_Should_Be_Called()
+ {
+ // Given
+ // When
+ this.bootstrapper.GetEngine();
+
+ // Then
+ this.bootstrapper.ApplicationContainerConfigured.ShouldBeTrue();
+ }
+
+ [Fact]
+ public void Get_Module_Gives_Same_Request_Lifetime_Instance_To_Each_Dependency()
+ {
+ // Given
+ this.bootstrapper.GetEngine();
+
+ // When
+ var result = this.bootstrapper.GetModule(typeof(FakeNancyModuleWithDependency), new NancyContext()) as FakeNancyModuleWithDependency;
+
+ // Then
+ result.FooDependency.ShouldNotBeNull();
+ result.FooDependency.ShouldBeSameAs(result.Dependency.FooDependency);
+ }
+
+ [Fact]
+ public void Should_not_return_the_same_instance_when_getmodule_is_called_multiple_times_with_different_context()
+ {
+ // Given
+ this.bootstrapper.GetEngine();
+ var context1 = new NancyContext();
+ var context2 = new NancyContext();
+
+ // When
+ var result = this.bootstrapper.GetModule(typeof(FakeNancyModuleWithDependency), context1) as FakeNancyModuleWithDependency;
+ var result2 = this.bootstrapper.GetModule(typeof(FakeNancyModuleWithDependency), context2) as FakeNancyModuleWithDependency;
+
+ // Then
+ result.FooDependency.ShouldNotBeNull();
+ result2.FooDependency.ShouldNotBeNull();
+ result.FooDependency.ShouldNotBeSameAs(result2.FooDependency);
}
}
}
View
24 src/Nancy.Bootstrappers.Ninject/NinjectNancyBootstrapper.cs
@@ -1,5 +1,6 @@
namespace Nancy.Bootstrappers.Ninject
{
+ using System;
using System.Collections.Generic;
using Diagnostics;
using Nancy.Bootstrapper;
@@ -48,15 +49,6 @@ protected override sealed INancyEngine GetEngineInternal()
}
/// <summary>
- /// Get the moduleKey generator
- /// </summary>
- /// <returns>IModuleKeyGenerator instance</returns>
- protected override sealed IModuleKeyGenerator GetModuleKeyGenerator()
- {
- return this.ApplicationContainer.Get<IModuleKeyGenerator>();
- }
-
- /// <summary>
/// Create a default, unconfigured, container
/// </summary>
/// <returns>Container instance</returns>
@@ -115,7 +107,7 @@ protected override sealed void RegisterRequestContainerModules(IKernel container
{
foreach (var moduleRegistrationType in moduleRegistrationTypes)
{
- container.Bind(typeof (INancyModule)).To(moduleRegistrationType.ModuleType).InSingletonScope().Named(moduleRegistrationType.ModuleKey);
+ container.Bind(typeof (INancyModule)).To(moduleRegistrationType.ModuleType).Named(moduleRegistrationType.ModuleType.FullName);
}
}
@@ -152,14 +144,16 @@ protected override sealed IEnumerable<INancyModule> GetAllModules(IKernel contai
}
/// <summary>
- /// Retreive a specific module instance from the container by its key
+ /// Retreive a specific module instance from the container
/// </summary>
/// <param name="container">Container to use</param>
- /// <param name="moduleKey">Module key of the module</param>
- /// <returns>NancyModule instance</returns>
- protected override sealed INancyModule GetModuleByKey(IKernel container, string moduleKey)
+ /// <param name="moduleType">Type of the module</param>
+ /// <returns>An <see cref="INancyModule"/> instance</returns>
+ protected override INancyModule GetModule(IKernel container, Type moduleType)
{
- return container.Get<INancyModule>(moduleKey);
+ container.Bind<INancyModule>().To(moduleType);
+
+ return container.Get(moduleType) as INancyModule;
}
}
}

0 comments on commit 98fc408

Please sign in to comment.