Skip to content
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
169 changes: 134 additions & 35 deletions source/developwithpassion.specification.specs/DependencyRegistrySpecs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using Machine.Fakes.Adapters.Rhinomocks;
using Machine.Specifications;
using developwithpassion.specifications.core;
using developwithpassion.specifications.faking;
Expand All @@ -9,81 +10,181 @@

namespace developwithpassion.specification.specs
{
[Subject(typeof(DependenciesRegistry))]
[Subject(typeof(DependenciesRegistry<RhinoFakeEngine>))]
public class DependencyRegistrySpecs
{
public abstract class concern : Observes
{
Establish c = () =>
{
the_connection = fake.an<IDbConnection>();
dependency_resolver = fake.an<IResolveADependencyForTheSUT>();
fake_gateway = fake.an<IManageFakes>();
dependencies = new Dictionary<Type, object>();
sut = new DependenciesRegistry(dependency_resolver,fake_gateway);
sut.downcast_to<DependenciesRegistry>().explicit_dependencies = dependencies;
dependencies = new Dictionary<Type, IDictionary<string, object>>();
sut = new DependenciesRegistry<RhinoFakeEngine>(dependency_resolver, fake_gateway);
sut.downcast_to<DependenciesRegistry<RhinoFakeEngine>>().explicit_dependencies = dependencies;
};

protected static IDictionary<Type, object> dependencies;
protected static Dictionary<Type, IDictionary<string, object>> dependencies;
protected static IManageTheDependenciesForASUT sut;
protected static IResolveADependencyForTheSUT dependency_resolver;
protected static IManageFakes fake_gateway;
protected static Dictionary<string, object> dependencies_by_name_for_idbconnection;
protected static IDbConnection the_connection;
}

public class when_asked_if_it_has_an_explicit_dependency :concern
public class when_creating_sut_with_a_default_dependency_for_idbconnection : concern
{
Establish c = () =>
{
dependencies.Add(typeof(IDbConnection), fake.an<IDbConnection>());
dependencies_by_name_for_idbconnection = new Dictionary<string, object>() { { sut.default_dependency_name, the_connection } };
dependencies.Add(typeof(IDbConnection), dependencies_by_name_for_idbconnection);
};

It should_make_the_decision_based_on_whether_an_explicit_dependency_was_registered = () =>
{
sut.has_been_provided_an(typeof(IDbConnection)).ShouldBeTrue();
sut.has_been_provided_an(typeof(IDbCommand)).ShouldBeFalse();
sut.has_been_provided_an(typeof(IDbConnection), "connection").ShouldBeTrue();
sut.has_been_provided_an(typeof(IDbConnection), sut.default_dependency_name).ShouldBeTrue();
sut.has_been_provided_an(typeof(IDbCommand), "command").ShouldBeFalse();
};


private static object result;
}

public class when_creating_sut_with_a_default_dependency_and_specific_dependency_for_idbconnection : concern
{
Establish c = () =>
{
specific_connection = fake.an<IDbConnection>();
dependencies_by_name_for_idbconnection = new Dictionary<string, object>()
{
{ sut.default_dependency_name, the_connection },
{ "connection", specific_connection}
};
dependencies.Add(typeof(IDbConnection), dependencies_by_name_for_idbconnection);
};

It should_make_the_decision_based_on_whether_an_explicit_dependency_was_registered = () =>
{
sut.has_been_provided_an(typeof(IDbConnection), "connection").ShouldBeTrue();
sut.has_been_provided_an(typeof(IDbConnection), sut.default_dependency_name).ShouldBeTrue();
sut.has_been_provided_an(typeof(IDbCommand), "command").ShouldBeFalse();
};

It should_have_expected_default_connection = () =>
{
sut.get_dependency_of(typeof(IDbConnection), sut.default_dependency_name).ShouldEqual(the_connection);
};

It should_have_expected_specific_connection = () =>
{
sut.get_dependency_of(typeof(IDbConnection), "connection").ShouldEqual(specific_connection);
};

static object result;
static IDbConnection specific_connection;
}

public class when_creating_sut_without_default_dependency_but_with_specific_dependency_for_idbconnection : concern
{
Establish c = () =>
{
specific_connection = fake.an<IDbConnection>();
dependencies_by_name_for_idbconnection = new Dictionary<string, object>() { { "connection", specific_connection} };
dependencies.Add(typeof(IDbConnection), dependencies_by_name_for_idbconnection);
};

It should_make_the_decision_based_on_whether_an_explicit_dependency_was_registered = () =>
{
sut.has_been_provided_an(typeof(IDbConnection), "connection").ShouldBeTrue();
sut.has_been_provided_an(typeof(IDbConnection), sut.default_dependency_name).ShouldBeFalse();
sut.has_been_provided_an(typeof(IDbCommand), "command").ShouldBeFalse();
};

It should_have_expected_specific_connection = () =>
{
sut.get_dependency_of(typeof(IDbConnection), "connection").ShouldEqual(specific_connection);
};

static object result;
static IDbConnection specific_connection;
}
public class when_getting_a_dependency:concern

public class when_getting_a_default_dependency : concern
{
public class and_the_dependency_has_been_explicitly_provided:when_getting_a_dependency
public class and_the_dependency_has_been_explicitly_provided_with_a_default : when_getting_a_default_dependency
{
private Establish c = () =>
{
dependencies_by_name_for_idbconnection = new Dictionary<string, object>() { { sut.default_dependency_name, the_connection } };
dependencies.Add(typeof(IDbConnection), dependencies_by_name_for_idbconnection);
};

Because b = () =>
result = sut.get_dependency_of(typeof(IDbConnection), "parameter_or_property_name");

It should_return_the_item_that_was_registered = () =>
result.ShouldEqual(the_connection);

static object result;
}

public class and_the_dependency_was_not_explicitly_provided : when_getting_a_default_dependency
{
Establish c = () =>
{
the_connection = fake.an<IDbConnection>();
dependencies.Add(typeof(IDbConnection),the_connection);
dependency_resolver.setup(x => x.resolve(typeof(IDbConnection))).Return(the_connection);
};

Because b = () =>
result = sut.get_dependency_of(typeof(IDbConnection));
result = sut.get_dependency_of(typeof(IDbConnection), "parameter_or_property_name");

It should_return_the_item_created_by_the_dependency_resolver = () =>
result.ShouldEqual(the_connection);

static object result;
}
}

public class when_getting_a_specific_dependency : concern
{
public class and_the_dependency_has_been_explicitly_provided_with_a_name : when_getting_a_default_dependency
{
private Establish c = () =>
{
dependencies_by_name_for_idbconnection = new Dictionary<string, object>() { { "connection", the_connection } };
dependencies.Add(typeof(IDbConnection), dependencies_by_name_for_idbconnection);
};

Because b = () =>
result = sut.get_dependency_of(typeof(IDbConnection), "connection");

It should_return_the_item_that_was_registered = () =>
result.ShouldEqual(the_connection);

static object result;
static IDbConnection the_connection;
}
}

public class and_the_dependency_was_not_explicitly_provided:when_getting_a_dependency
public class and_the_dependency_was_not_explicitly_provided : when_getting_a_default_dependency
{
Establish c = () =>
{
the_connection = fake.an<IDbConnection>();
dependency_resolver.setup(x => x.resolve(typeof(IDbConnection))).Return(the_connection);
};

Because b = () =>
result = sut.get_dependency_of(typeof(IDbConnection));
result = sut.get_dependency_of(typeof(IDbConnection), "connection");

It should_return_the_item_created_by_the_dependency_resolver = () =>
result.ShouldEqual(the_connection);

static object result;
static IDbConnection the_connection;
}
}
}
public class when_storing_a_dependency : concern

public class when_storing_default_dependencies : concern
{
public class and_it_has_not_already_been_stored : when_storing_a_dependency
public class and_it_has_not_already_been_stored : when_storing_default_dependencies
{
Establish c = () =>
{
Expand All @@ -93,39 +194,37 @@ public class and_it_has_not_already_been_stored : when_storing_a_dependency
Because b = () =>
result = sut.on(the_connection);

It should_be_stored_in_the_underlying_dependencies = () =>
dependencies[typeof(IDbConnection)].ShouldEqual(the_connection);
private It should_be_stored_in_the_default_underlying_dependencies = () =>
dependencies[typeof(IDbConnection)][sut.default_dependency_name].ShouldEqual(the_connection);

It should_return_the_instance_being_registered = () =>
result.ShouldEqual(the_connection);

static IDbConnection the_connection;
static IDbConnection result;
}
public class and_it_has_already_been_stored : when_storing_a_dependency
public class and_it_has_already_been_stored : when_storing_default_dependencies
{
Establish c = () =>
{
the_connection = fake.an<IDbConnection>();
the_new_connection = fake.an<IDbConnection>();
sut.on(the_connection);
};

Because b = () =>
result = sut.on(the_new_connection);

It should_replace_the_existing_instance = () =>
dependencies[typeof(IDbConnection)].ShouldEqual(the_new_connection);
It should_replace_the_existing_default_instance = () =>
dependencies[typeof(IDbConnection)][sut.default_dependency_name].ShouldEqual(the_new_connection);

It should_return_the_instance_being_registered = () =>
result.ShouldEqual(the_new_connection);

static IDbConnection the_connection;
static IDbConnection result;
static IDbConnection the_new_connection;
}

public class and_the_dependency_is_not_being_provided:when_storing_a_dependency
public class and_the_dependency_is_not_being_provided : when_storing_default_dependencies
{
Establish c = () =>
{
Expand All @@ -136,12 +235,12 @@ public class and_the_dependency_is_not_being_provided:when_storing_a_dependency
Because b = () =>
result = sut.on<IDbConnection>();

It should_store_the_item_created_by_the_fake_gateway = () =>
dependencies[typeof(IDbConnection)].ShouldEqual(the_connection_created_by_the_fake_gateway);
private It should_store_the_item_created_by_the_fake_gateway_as_the_default = () =>
dependencies[typeof(IDbConnection)][sut.default_dependency_name].ShouldEqual(the_connection_created_by_the_fake_gateway);

It should_return_the_item_created_by_the_fake_gateway = () =>
result = the_connection_created_by_the_fake_gateway;


static IDbConnection the_connection_created_by_the_fake_gateway;
static IDbConnection result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public class when_visting_an_item_that_inherits : concern
Establish c = () =>
{
item = new ItemThatInherits();
dependency_registry.setup(x => x.has_been_provided_an(typeof(IDbConnection)))
dependency_registry.setup(x => x.has_been_provided_an(typeof(IDbConnection), "connection"))
.Return(true);
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbConnection)))
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbConnection), "connection"))
.Return(fake.an<IDbConnection>());
};

Expand Down Expand Up @@ -67,9 +67,9 @@ public class when_visting_an_item_that_has_non_public_accessor : concern
Establish c = () =>
{
item = new ItemToUpdate();
dependency_registry.setup(x => x.has_been_provided_an(typeof(IDbConnection)))
dependency_registry.setup(x => x.has_been_provided_an(typeof(IDbConnection), "connection"))
.Return(true);
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbConnection)))
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbConnection), "connection"))
.Return(fake.an<IDbConnection>());
};

Expand All @@ -91,8 +91,8 @@ public class when_the_dependency_manager_throws_an_exception_while_trying_to_get
Establish c = () =>
{
original_exception = new Exception();
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbConnection))).Throw(original_exception);
dependency_registry.setup(x => x.get_dependency_of(typeof(IDataAdapter))).Return(fake.an<IDataAdapter>());
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbConnection), "connection")).Throw(original_exception);
dependency_registry.setup(x => x.get_dependency_of(typeof(IDataAdapter), "adapter")).Return(fake.an<IDataAdapter>());
item = new AnItem();
};

Expand Down Expand Up @@ -127,7 +127,7 @@ public class and_a_value_has_been_registered_in_the_registry : and_the_accessors
Establish c = () =>
{
the_connection_from_the_registry = fake.an<IDbConnection>();
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbConnection))).Return(
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbConnection), "connection")).Return(
the_connection_from_the_registry);
item = new ItemToUpdate();
};
Expand Down Expand Up @@ -167,8 +167,8 @@ public class and_the_dependencies_were_explicitly_specified_in_the_registry :
Establish c = () =>
{
the_connection_from_the_registry = fake.an<IDbConnection>();
dependency_registry.setup(x => x.has_been_provided_an(typeof(IDbConnection))).Return(true);
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbConnection))).Return(
dependency_registry.setup(x => x.has_been_provided_an(typeof(IDbConnection), "connection")).Return(true);
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbConnection), "connection")).Return(
the_connection_from_the_registry);
original_connection = fake.an<IDbConnection>();
item = new ItemToUpdate {connection = original_connection};
Expand Down Expand Up @@ -199,8 +199,8 @@ public class ItemWithInitializer
Establish c = () =>
{
item = new SomeItem();
dependency_registry.setup(x => x.has_been_provided_an(typeof(SomeItem))).Return(true);
dependency_registry.setup(x => x.get_dependency_of(typeof(SomeItem))).Return(
dependency_registry.setup(x => x.has_been_provided_an(typeof(SomeItem), "some_item")).Return(true);
dependency_registry.setup(x => x.get_dependency_of(typeof(SomeItem), "some_item")).Return(
item);
target = new ItemWithInitializer();
};
Expand Down
10 changes: 6 additions & 4 deletions source/developwithpassion.specification.specs/SUTFactorySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class concern : Observes
command = fake.an<IDbCommand>();
non_ctor_dependency_visitor = fake.an<IUpdateNonCtorDependenciesOnAnItem>();
dependency_registry = fake.an<IManageTheDependenciesForASUT>();
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbConnection))).Return(connection);
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbCommand))).Return(command);
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbConnection), "connection")).Return(connection);
dependency_registry.setup(x => x.get_dependency_of(typeof(IDbCommand), "command")).Return(command);
};

protected static DefaultSUTFactory<ItemToBeCreated> create_sut<ItemToBeCreated>()
Expand Down Expand Up @@ -97,7 +97,7 @@ public class and_arguments_have_not_been_specifically_provided :
when_creating_a_type_that_has_constructor_parameters_that_cant_be_faked
{
Establish c = () =>
dependency_registry.setup(x => x.get_dependency_of(typeof(SomeOtherType))).Throw(original_exception);
dependency_registry.setup(x => x.get_dependency_of(typeof(SomeOtherType), "other")).Throw(original_exception);

Because b = () =>
spec.catch_exception(() => sut.create());
Expand All @@ -112,7 +112,7 @@ public class and_the_arguments_have_been_specifically_provided :
Establish c = () =>
{
the_item = new SomeOtherType(3);
dependency_registry.setup(x => x.get_dependency_of(typeof(SomeOtherType))).Return(the_item);
dependency_registry.setup(x => x.get_dependency_of(typeof(SomeOtherType), "other")).Return(the_item);
};

Because b = () =>
Expand All @@ -133,7 +133,9 @@ public class when_creating_an_item_and_no_constructor_arguments_have_been_provid
};

Because b = () =>
{
result = sut.create();
};

static ItemToCreate created_item;
static ItemToCreate result;
Expand Down
Loading