Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dependency injections for test. (Ninject.MockingKernel.Moq) #661

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Source/Bifrost.Specs/Bifrost.Specs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@
<Compile Include="Execution\for_DefaultConvention\when_resolving_an_interface_with_an_implementation_following_convention_with_a_singleton_attribute.cs" />
<Compile Include="Execution\for_ExecutionContextDetailsPopulator\when_populating_and_two_populators_exist.cs" />
<Compile Include="Execution\for_ExecutionContextFactory\when_creating.cs" />
<Compile Include="Execution\for_ExecutionContextManager\given\all_dependencies.cs" />
<Compile Include="Execution\for_ExecutionContextManager\given\an_execution_context_manager.cs" />
<Compile Include="Execution\for_ExecutionContextManager\when_getting_current_twice.cs" />
<Compile Include="Execution\for_InstancesOf\IAmAnInterface.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
using Bifrost.Execution;
using Bifrost.Testing;
using Machine.Specifications;
using Moq;

namespace Bifrost.Specs.Execution.for_BindingConventionManager.given
{

public class a_binding_convention_manager
public class a_binding_convention_manager : dependency_injection
{
protected static BindingConventionManager manager;
protected static Mock<IContainer> container_mock;
protected static Mock<ITypeDiscoverer> type_discoverer_mock;


Establish context = () =>
{
container_mock = new Mock<IContainer>();
type_discoverer_mock = new Mock<ITypeDiscoverer>();
manager = new BindingConventionManager(container_mock.Object, type_discoverer_mock.Object);
};
Establish context = () => manager = Get<BindingConventionManager>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Bifrost.Execution;
using Machine.Specifications;

namespace Bifrost.Specs.Execution.for_BindingConventionManager.given
Expand All @@ -8,9 +9,9 @@ public class a_binding_convention_manager_with_one_type : a_binding_convention_m
protected static Type service_type;

Establish context = () =>
{
service_type = typeof (IService);
type_discoverer_mock.Setup(t => t.GetAll()).Returns(new[] {service_type});
};
{
service_type = typeof (IService);
GetMock<ITypeDiscoverer>().Setup(t => t.GetAll()).Returns(new[] {service_type});
};
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
using System;
using Bifrost.Execution;
using Machine.Specifications;
using Moq;
using It = Machine.Specifications.It;

namespace Bifrost.Specs.Execution.for_BindingConventionManager
{
public class when_adding_same_convention_twice : given.a_binding_convention_manager_with_one_type
{
static Mock<IBindingConvention> convention_mock;
static Type convention_type;

Establish context = () =>
{
convention_mock = new Mock<IBindingConvention>();

convention_type = convention_mock.Object.GetType();
container_mock.Setup(c => c.Get(convention_type)).Returns(convention_mock.Object);
};
{
convention_type = Get<IBindingConvention>().GetType();
GetMock<IContainer>().Setup(c => c.Get(convention_type)).Returns(Get<IBindingConvention>());
};

Because of = () =>
{
manager.Add(convention_type);
manager.Add(convention_type);
manager.Initialize();
};
{
manager.Add(convention_type);
manager.Add(convention_type);
manager.Initialize();
};

It should_not_try_to_resolve_twice = () => convention_mock.Verify(c => c.CanResolve(container_mock.Object, service_type), Times.Once());
It should_not_try_to_resolve_twice = () => GetMock<IBindingConvention>().Verify(c => c.CanResolve(Get<IContainer>(), service_type), Moq.Times.Once());
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
using System;
using Bifrost.Execution;
using Machine.Specifications;
using Moq;
using It = Machine.Specifications.It;

namespace Bifrost.Specs.Execution.for_BindingConventionManager
{
public class when_automatically_discovering_conventions_and_initializing : given.a_binding_convention_manager_with_one_type
{
static Mock<IBindingConvention> convention_mock;
static Type convention_type;

Establish context = () =>
{
convention_mock = new Mock<IBindingConvention>();
convention_mock.Setup(c => c.CanResolve(container_mock.Object, service_type)).Returns(true);
convention_type = convention_mock.Object.GetType();
container_mock.Setup(c => c.Get(convention_type)).Returns(convention_mock.Object);
{
GetMock<IBindingConvention>().Setup(c => c.CanResolve(Get<IContainer>(), service_type)).Returns(true);
convention_type = Get<IBindingConvention>().GetType();
GetMock<IContainer>().Setup(c => c.Get(convention_type)).Returns(Get<IBindingConvention>());

type_discoverer_mock.Setup(t => t.FindMultiple<IBindingConvention>()).Returns(new[] {convention_type});
};
GetMock<ITypeDiscoverer>().Setup(t => t.FindMultiple<IBindingConvention>()).Returns(new[] {convention_type});
};

Because of = () => manager.DiscoverAndInitialize();

It should_ask_if_convention_can_resolve = () => convention_mock.Verify(c => c.CanResolve(container_mock.Object, service_type), Times.Once());
It should_ask_if_convention_can_resolve = () => GetMock<IBindingConvention>().Verify(c => c.CanResolve(Get<IContainer>(), service_type), Moq.Times.Once());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using Bifrost.Execution;
using Machine.Specifications;
using Moq;
using It = Machine.Specifications.It;

namespace Bifrost.Specs.Execution.for_BindingConventionManager
{
Expand All @@ -11,13 +9,12 @@ public class when_initializing_with_one_convention : given.a_binding_convention_
static Type convention_type;

Establish context = () =>
{
var convention_mock = new Mock<IBindingConvention>();
convention_type = convention_mock.Object.GetType();
manager.Add(convention_type);
};
{
convention_type = Get<IBindingConvention>().GetType();
manager.Add(convention_type);
};
Because of = () => manager.Initialize();

It should_get_an_instance_of_the_convention = () => container_mock.Verify(c => c.Get(convention_type), Times.Once());
It should_get_an_instance_of_the_convention = () => GetMock<IContainer>().Verify(c => c.Get(convention_type), Moq.Times.Once());
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
using System;
using Bifrost.Execution;
using Machine.Specifications;
using Moq;
using It = Machine.Specifications.It;

namespace Bifrost.Specs.Execution.for_BindingConventionManager
{
public class when_initializing_with_existing_services_bound_and_one_convention : given.a_binding_convention_manager_with_one_type
{
static Mock<IBindingConvention> convention_mock;
static Type convention_type;


Establish context = () =>
{
convention_mock = new Mock<IBindingConvention>();
convention_type = convention_mock.Object.GetType();
container_mock.Setup(c => c.GetBoundServices()).Returns(new[] {service_type});
container_mock.Setup(c => c.Get(convention_type)).Returns(convention_mock.Object);
manager.Add(convention_type);
};
{
convention_type = Get<IBindingConvention>().GetType();
GetMock<IContainer>().Setup(c => c.GetBoundServices()).Returns(new[] {service_type});
GetMock<IContainer>().Setup(c => c.Get(convention_type)).Returns(Get<IBindingConvention>());
manager.Add(convention_type);
};
Because of = () => manager.Initialize();

It should_not_ask_to_resolve = () => convention_mock.Verify(c => c.CanResolve(container_mock.Object, service_type), Times.Never());
It should_not_ask_to_resolve = () => GetMock<IBindingConvention>().Verify(c => c.CanResolve(Get<IContainer>(), service_type), Moq.Times.Never());
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
using System;
using Bifrost.Execution;
using Machine.Specifications;
using Moq;
using It = Machine.Specifications.It;


namespace Bifrost.Specs.Execution.for_BindingConventionManager
{
public class when_initializing_with_one_convention_and_one_type : given.a_binding_convention_manager_with_one_type
{
static Mock<IBindingConvention> convention_mock;
static Type convention_type;

Establish context = () =>
{
convention_mock = new Mock<IBindingConvention>();
convention_type = convention_mock.Object.GetType();
manager.Add(convention_type);
container_mock.Setup(c => c.Get(convention_type)).Returns(convention_mock.Object);
convention_mock.Setup(c => c.CanResolve(container_mock.Object, service_type)).Returns(true);
};
{
convention_type = Get<IBindingConvention>().GetType();
manager.Add(convention_type);
GetMock<IContainer>().Setup(c => c.Get(convention_type)).Returns(Get<IBindingConvention>());
GetMock<IBindingConvention>().Setup(c => c.CanResolve(Get<IContainer>(), service_type)).Returns(true);
};

Because of = () => manager.Initialize();

It should_resolve = () => convention_mock.Verify(c => c.Resolve(container_mock.Object, service_type));
It should_resolve = () => GetMock<IBindingConvention>().Verify(c => c.Resolve(Get<IContainer>(), service_type));
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
using System;
using Bifrost.Execution;
using Machine.Specifications;
using Moq;
using It = Machine.Specifications.It;

namespace Bifrost.Specs.Execution.for_BindingConventionManager
{
public class when_initializing_with_two_conventions_and_one_type_and_first_convention_can_resolve : given.a_binding_convention_manager_with_one_type
{
static Mock<IBindingConvention> first_convention_mock;
static Type first_convention_type;

static FakeBindingConvention second_convention;
static Type second_convention_type;

Establish context = () =>
{
first_convention_mock = new Mock<IBindingConvention>();
first_convention_type = first_convention_mock.Object.GetType();
manager.Add(first_convention_type);
container_mock.Setup(c => c.Get(first_convention_type)).Returns(first_convention_mock.Object);
first_convention_mock.Setup(c => c.CanResolve(container_mock.Object, service_type)).Returns(true);
{
first_convention_type = Get<IBindingConvention>().GetType();
manager.Add(first_convention_type);
GetMock<IContainer>().Setup(c => c.Get(first_convention_type)).Returns(Get<IBindingConvention>());
GetMock<IBindingConvention>().Setup(c => c.CanResolve(Get<IContainer>(), service_type)).Returns(true);

second_convention = new FakeBindingConvention();
second_convention_type = typeof (FakeBindingConvention);
manager.Add(second_convention_type);
container_mock.Setup(c => c.Get(second_convention_type)).Returns(second_convention);
second_convention.CanResolveProperty = false;
};
second_convention = new FakeBindingConvention();
second_convention_type = typeof (FakeBindingConvention);
manager.Add(second_convention_type);
GetMock<IContainer>().Setup(c => c.Get(second_convention_type)).Returns(second_convention);
second_convention.CanResolveProperty = false;
};

Because of = () => manager.Initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,28 @@ namespace Bifrost.Specs.Execution.for_BindingConventionManager
{
public class when_initializing_with_two_conventions_and_one_type_and_second_convention_can_resolve_but_not_the_first : given.a_binding_convention_manager_with_one_type
{
static Mock<IBindingConvention> first_convention_mock;
static Type first_convention_type;

static FakeBindingConvention second_convention;
static Type second_convention_type;

Establish context = () =>
{
first_convention_mock = new Mock<IBindingConvention>();
first_convention_type = first_convention_mock.Object.GetType();
manager.Add(first_convention_type);
container_mock.Setup(c => c.Get(first_convention_type)).Returns(first_convention_mock.Object);
first_convention_mock.Setup(c => c.CanResolve(container_mock.Object, service_type)).Returns(false);
{
first_convention_type = Get<IBindingConvention>().GetType();
manager.Add(first_convention_type);
GetMock<IContainer>().Setup(c => c.Get(first_convention_type)).Returns(Get<IBindingConvention>());
GetMock<IBindingConvention>().Setup(c => c.CanResolve(Get<IContainer>(), service_type)).Returns(false);

second_convention = new FakeBindingConvention();
second_convention_type = typeof(FakeBindingConvention);
manager.Add(second_convention_type);
container_mock.Setup(c => c.Get(second_convention_type)).Returns(second_convention);
second_convention.CanResolveProperty = true;
};
second_convention = new FakeBindingConvention();
second_convention_type = typeof (FakeBindingConvention);
manager.Add(second_convention_type);
GetMock<IContainer>().Setup(c => c.Get(second_convention_type)).Returns(second_convention);
second_convention.CanResolveProperty = true;
};

Because of = () => manager.Initialize();

It should_not_resolve_on_first_convention = () => first_convention_mock.Verify(c => c.Resolve(container_mock.Object, service_type), Times.Never());
It should_not_resolve_on_first_convention = () => GetMock<IBindingConvention>().Verify(c => c.Resolve(Get<IContainer>(), service_type), Times.Never());
It should_ask_if_it_can_resolve_on_second_convention = () => second_convention.CanResolveCalled.ShouldBeTrue();
It should_resolve_on_second_convention = () => second_convention.ResolveCalled.ShouldBeTrue();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
using System.Dynamic;
using Bifrost.Execution;
using Bifrost.Testing;
using Machine.Specifications;
using Moq;
using It = Machine.Specifications.It;

namespace Bifrost.Specs.Execution.for_ExecutionContextDetailsPopulator
{
public class when_populating_and_two_populators_exist
public class when_populating_and_two_populators_exist : dependency_injection
{
static Mock<ITypeDiscoverer> type_discoverer_mock;
static ExecutionContextDetailsPopulator populator;
static Mock<IExecutionContext> execution_context_mock;
static Mock<IContainer> container_mock;
static ExpandoObject details;

static Mock<ICanPopulateExecutionContextDetails> first_populator;
static Mock<ICanPopulateExecutionContextDetails> second_populator;

Establish context = () =>
{
type_discoverer_mock = new Mock<ITypeDiscoverer>();
execution_context_mock = new Mock<IExecutionContext>();
details = new ExpandoObject();
container_mock = new Mock<IContainer>();

type_discoverer_mock.Setup(t => t.FindMultiple<ICanPopulateExecutionContextDetails>()).Returns(new[] { typeof(string), typeof(object) });
GetMock<ITypeDiscoverer>().Setup(t => t.FindMultiple<ICanPopulateExecutionContextDetails>()).Returns(new[] { typeof(string), typeof(object) });

first_populator = new Mock<ICanPopulateExecutionContextDetails>();
second_populator = new Mock<ICanPopulateExecutionContextDetails>();

container_mock.Setup(c => c.Get(typeof(string))).Returns(first_populator.Object);
container_mock.Setup(c => c.Get(typeof(object))).Returns(second_populator.Object);
GetMock<IContainer>().Setup(c => c.Get(typeof(string))).Returns(first_populator.Object);
GetMock<IContainer>().Setup(c => c.Get(typeof(object))).Returns(second_populator.Object);

populator = new ExecutionContextDetailsPopulator(type_discoverer_mock.Object, container_mock.Object);
populator = Get<ExecutionContextDetailsPopulator>();
};

Because of = () => populator.Populate(execution_context_mock.Object, details);
Because of = () => populator.Populate(Get<IExecutionContext>(), details);

It should_call_populate_on_first_populator = () => first_populator.Verify(p => p.Populate(execution_context_mock.Object, details));
It should_call_populate_on_second_populator = () => second_populator.Verify(p => p.Populate(execution_context_mock.Object, details));
It should_call_populate_on_first_populator = () => first_populator.Verify(p => p.Populate(Get<IExecutionContext>(), details));
It should_call_populate_on_second_populator = () => second_populator.Verify(p => p.Populate(Get<IExecutionContext>(), details));
}
}
Loading