Skip to content

Commit

Permalink
Improve test coverage for ReadOnlyKernel.
Browse files Browse the repository at this point in the history
Added tests for #341, #340, #333.
  • Loading branch information
drieseng authored and scott-xu committed Jan 15, 2022
1 parent 1405861 commit 59dc405
Show file tree
Hide file tree
Showing 14 changed files with 1,208 additions and 5 deletions.
76 changes: 74 additions & 2 deletions src/Ninject.Test/Integration/ConstantTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Ninject.Tests.Integration.ConstantTests
{
using System;

using System.Collections.Generic;
using Fakes;
using FluentAssertions;
using Xunit;
Expand All @@ -24,7 +24,7 @@ public void Dispose()
public class WhenTypeIsBoundToAConstant : ConstantContext
{
[Fact]
public void TheSameInstanceShouldBeReturned()
public void SameInstanceShouldBeReturnedWhenConstantIsSingularService()
{
var sword = new Sword();
this.kernel.Bind<IWeapon>().ToConstant(sword);
Expand All @@ -33,6 +33,78 @@ public void TheSameInstanceShouldBeReturned()
instance.Should().BeSameAs(sword);
}

[Fact(Skip = "https://github.com/ninject/Ninject/issues/333")]
public void SameInstanceShouldBeReturnedWhenConstantIsArray()
{
var swords = new Sword[0];
this.kernel.Bind<Sword[]>().ToConstant(swords);

var instance = this.kernel.Get<Sword[]>();
instance.Should().BeSameAs(swords);
}

[Fact(Skip = "https://github.com/ninject/Ninject/issues/333")]
public void SameInstanceShouldBeReturnedWhenConstantIsGenericList()
{
var swords = new List<Sword>();
this.kernel.Bind<List<Sword>>().ToConstant(swords);

var instance = this.kernel.Get<List<Sword>>();
instance.Should().BeSameAs(swords);
}

[Fact(Skip = "https://github.com/ninject/Ninject/issues/333")]
public void SameInstanceShouldBeReturnedWhenConstantIsGenericICollection()
{
var swords = new List<Sword>();
this.kernel.Bind<ICollection<Sword>>().ToConstant(swords);

var instance = this.kernel.Get<ICollection<Sword>>();
instance.Should().BeSameAs(swords);
}

[Fact(Skip = "https://github.com/ninject/Ninject/issues/333")]
public void SameInstanceShouldBeReturnedWhenConstantIsGenericIList()
{
var swords = new List<Sword>();
this.kernel.Bind<IList<Sword>>().ToConstant(swords);

var instance = this.kernel.Get<IList<Sword>>();
instance.Should().BeSameAs(swords);
}


[Fact]
public void ConditionalBindingShouldNotAffectUnconditionalBinding()
{
var sword = new Sword();
this.kernel.Bind<IWeapon>().ToConstant(sword).WhenInjectedInto<Samurai>();
this.kernel.Bind<IWeapon>().To<Shuriken>();

var samurai = this.kernel.Get<Samurai>();
samurai.Weapon.Should().BeSameAs(sword);
var weapon = this.kernel.Get<IWeapon>();
weapon.Should().BeOfType<Shuriken>();
}

[Fact]
public void TheBindingShouldOnlyBeResolvedOnce()
{
var builder = this.kernel.Bind<IWeapon>().ToConstant(new Sword());
var provider = new ResolveCountingProvider(builder.BindingConfiguration.ProviderCallback);
builder.BindingConfiguration.ProviderCallback = ctx => provider.Callback(ctx);


this.kernel.Get<IWeapon>();
this.kernel.Get<IWeapon>();

provider.Count.Should().Be(1);
}
}

public class WhenTypeIsBoundToAGenericList : ConstantContext
{

[Fact]
public void ConditionalBindingShouldNotAffectUnconditionalBinding()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Ninject.Tests.Integration.EnumerableDependenciesTests.Fakes
{
using System.Collections.Generic;
using System.Linq;

public class RequestsConstrainedICollection : IParent
{
public RequestsConstrainedICollection([Named("bob")] ICollection<IChild> children)
{
this.Children = children.ToList();
}

public IList<IChild> Children { get; private set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Ninject.Tests.Integration.EnumerableDependenciesTests.Fakes
{
using System.Collections.Generic;

public class RequestsConstrainedIList : IParent
{
public RequestsConstrainedIList([Named("bob")] IList<IChild> children)
{
this.Children = children;
}

public IList<IChild> Children { get; private set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Ninject.Tests.Integration.EnumerableDependenciesTests.Fakes
{
using System.Collections.Generic;
using System.Linq;

public class RequestsICollection : IParent
{
public RequestsICollection(ICollection<IChild> children)
{
this.Children = children.ToList();
}

public IList<IChild> Children { get; private set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Ninject.Tests.Integration.EnumerableDependenciesTests.Fakes
{
using System.Collections.Generic;

public class RequestsIList : IParent
{
public RequestsIList(IList<IChild> children)
{
this.Children = children;
}

public IList<IChild> Children { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using FluentAssertions;
using Ninject.Tests.Integration.EnumerableDependenciesTests.Fakes;
using System.Collections.Generic;
using Xunit;

public class WhenServiceRequestsConstrainedEnumerableOfDependencies : ConstrainedDependenciesContext
Expand All @@ -16,6 +17,7 @@ public void ServiceIsInjectedWithAllDependenciesThatMatchTheConstraint()
var parent = this.Kernel.Get<IParent>();

VerifyInjection(parent);
parent.Children.Should().BeOfType<List<IChild>>();
}

[Fact]
Expand All @@ -28,6 +30,7 @@ public void WhenNoMatchingBindingExistsEmptyEnumerableIsInjected()
var parent = this.Kernel.Get<IParent>();

parent.Should().NotBeNull();
parent.Children.Should().BeOfType<List<IChild>>();
parent.Children.Count.Should().Be(0);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Ninject.Tests.Integration.EnumerableDependenciesTests
{
using FluentAssertions;
using Ninject.Tests.Integration.EnumerableDependenciesTests.Fakes;
using System.Collections.Generic;
using Xunit;

public class WhenServiceRequestsConstrainedICollectionOfDependencies : ConstrainedDependenciesContext
{
[Fact]
public void ServiceIsInjectedWithAllDependenciesThatMatchTheConstraint()
{
this.Kernel.Bind<IParent>().To<RequestsConstrainedICollection>();
this.Kernel.Bind<IChild>().To<ChildA>().Named("joe");
this.Kernel.Bind<IChild>().To<ChildB>().Named("bob");

var parent = this.Kernel.Get<IParent>();

VerifyInjection(parent);
parent.Children.Should().BeOfType<List<IChild>>();
}

[Fact]
public void WhenNoMatchingBindingExistsEmptyEnumerableIsInjected()
{
this.Kernel.Bind<IParent>().To<RequestsConstrainedICollection>();
this.Kernel.Bind<IChild>().To<ChildA>().Named("joe");
this.Kernel.Bind<IChild>().To<ChildB>().Named("ian");

var parent = this.Kernel.Get<IParent>();

parent.Should().NotBeNull();
parent.Children.Should().BeOfType<List<IChild>>();
parent.Children.Count.Should().Be(0);
parent.Children.Should().BeOfType<List<IChild>>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Ninject.Tests.Integration.EnumerableDependenciesTests
{
using FluentAssertions;
using Ninject.Tests.Integration.EnumerableDependenciesTests.Fakes;
using System.Collections.Generic;
using Xunit;

public class WhenServiceRequestsConstrainedIListOfDependencies : ConstrainedDependenciesContext
{
[Fact]
public void ServiceIsInjectedWithAllDependenciesThatMatchTheConstraint()
{
this.Kernel.Bind<IParent>().To<RequestsConstrainedIList>();
this.Kernel.Bind<IChild>().To<ChildA>().Named("joe");
this.Kernel.Bind<IChild>().To<ChildB>().Named("bob");

var parent = this.Kernel.Get<IParent>();

VerifyInjection(parent);
parent.Children.Should().BeOfType<List<IChild>>();
}

[Fact]
public void WhenNoMatchingBindingExistsEmptyEnumerableIsInjected()
{
this.Kernel.Bind<IParent>().To<RequestsConstrainedIList>();
this.Kernel.Bind<IChild>().To<ChildA>().Named("joe");
this.Kernel.Bind<IChild>().To<ChildB>().Named("ian");

var parent = this.Kernel.Get<IParent>();

parent.Should().NotBeNull();
parent.Children.Should().BeOfType<List<IChild>>();
parent.Children.Count.Should().Be(0);
parent.Children.Should().BeOfType<List<IChild>>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
{
using FluentAssertions;
using Ninject.Tests.Integration.EnumerableDependenciesTests.Fakes;
using System.Collections.Generic;
using Xunit;

public class WhenServiceRequestsConstrainedListOfDependencies : ConstrainedDependenciesContext
public class WhenServiceRequestsConstrainedListOfDependencies : ConstrainedDependenciesContext
{
[Fact]
public void ServiceIsInjectedWithAllDependenciesThatMatchTheConstraint()
Expand All @@ -16,6 +17,7 @@ public void ServiceIsInjectedWithAllDependenciesThatMatchTheConstraint()
var parent = this.Kernel.Get<IParent>();

VerifyInjection(parent);
parent.Children.Should().BeOfType<List<IChild>>();
}

[Fact]
Expand All @@ -28,6 +30,7 @@ public void WhenNoMatchingBindingExistsEmptyEnumerableIsInjected()
var parent = this.Kernel.Get<IParent>();

parent.Should().NotBeNull();
parent.Children.Should().BeOfType<List<IChild>>();
parent.Children.Count.Should().Be(0);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace Ninject.Tests.Integration.EnumerableDependenciesTests
{
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Ninject.Tests.Integration.EnumerableDependenciesTests.Fakes;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace Ninject.Tests.Integration.EnumerableDependenciesTests
{
using FluentAssertions;
using Ninject.Tests.Integration.EnumerableDependenciesTests.Fakes;
using System.Collections.Generic;
using Xunit;

public class WhenServiceRequestsUnconstrainedIListOfDependencies : UnconstrainedDependenciesContext
{
[Fact]
public void ServiceIsInjectedWithListOfAllAvailableDependencies()
{
this.Kernel.Bind<IParent>().To<RequestsIList>();
this.Kernel.Bind<IChild>().To<ChildA>();
this.Kernel.Bind<IChild>().To<ChildB>();

var parent = this.Kernel.Get<IParent>();

VerifyInjection(parent);
parent.Children.Should().BeOfType<List<IChild>>();
}

[Fact]
public void ServiceIsInjectedWithEmptyListIfElementTypeIsMissingBinding()
{
this.Kernel.Bind<IParent>().To<RequestsIList>();

var parent = this.Kernel.Get<IParent>();

parent.Should().NotBeNull();
parent.Children.Should().BeEmpty();
parent.Children.Should().BeOfType<List<IChild>>();
}

[Fact]
public void ListIsResolvedIfElementTypeIsExplicitlyBinded()
{
this.Kernel.Bind<IChild>().To<ChildA>();

var children = this.Kernel.Get<IList<IChild>>();

children.Should().NotBeEmpty();
children.Should().BeOfType<List<IChild>>();
}

[Fact]
public void EmptyListIsResolvedIfElementTypeIsMissingBinding()
{
var children = this.Kernel.Get<IList<IChild>>();

children.Should().BeEmpty();
children.Should().BeOfType<List<IChild>>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using FluentAssertions;
using Ninject.Tests.Integration.EnumerableDependenciesTests.Fakes;
using System.Collections.Generic;
using Xunit;

public class WhenServiceRequestsUnconstrainedListOfDependencies : UnconstrainedDependenciesContext
Expand Down Expand Up @@ -40,5 +41,23 @@ public void ServiceIsInjectedWithEmptyListIfElementTypeIsMissingBinding()
parent.Should().NotBeNull();
parent.Children.Should().BeEmpty();
}

[Fact]
public void ListIsResolvedIfElementTypeIsExplicitlyBinded()
{
this.Kernel.Bind<IChild>().To<ChildA>();

var children = this.Kernel.Get<List<IChild>>();

children.Should().NotBeEmpty();
}

[Fact]
public void EmptyListIsResolvedIfElementTypeIsMissingBinding()
{
var children = this.Kernel.Get<List<IChild>>();

children.Should().BeEmpty();
}
}
}
Loading

0 comments on commit 59dc405

Please sign in to comment.