From 7c3da47323713d260050a7f09ca6c45b98d53495 Mon Sep 17 00:00:00 2001 From: Sean Bennett Date: Sun, 2 Jun 2013 09:01:28 -0600 Subject: [PATCH 1/7] Update references of developwithpassion.specifications.examples to fix build without requiring nuget to run --- .../developwithpassion.specifications.examples.csproj | 8 +++++--- .../packages.config | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/source/developwithpassion.specifications.examples/developwithpassion.specifications.examples.csproj b/source/developwithpassion.specifications.examples/developwithpassion.specifications.examples.csproj index 5d6b899..6183720 100755 --- a/source/developwithpassion.specifications.examples/developwithpassion.specifications.examples.csproj +++ b/source/developwithpassion.specifications.examples/developwithpassion.specifications.examples.csproj @@ -31,12 +31,11 @@ 4 - - False + ..\..\packages\Machine.Fakes.1.4.0\lib\net40\Machine.Fakes.dll - ..\..\packages\Machine.Fakes.RhinoMocks.0.5.1\lib\net40\Machine.Fakes.Adapters.RhinoMocks.dll + ..\..\packages\Machine.Fakes.RhinoMocks.1.4.0\lib\net40\Machine.Fakes.Adapters.RhinoMocks.dll False @@ -83,6 +82,9 @@ developwithpassion.specifications + + + + diff --git a/source/developwithpassion.specifications.examples/automatic_sut_creation/with_a_mix_of_fakeable_and_two_non_fakeable_of_the_same_type_contructor_parameters.cs b/source/developwithpassion.specifications.examples/automatic_sut_creation/with_a_mix_of_fakeable_and_two_non_fakeable_of_the_same_type_contructor_parameters.cs index 5bb2519..a78f0f6 100644 --- a/source/developwithpassion.specifications.examples/automatic_sut_creation/with_a_mix_of_fakeable_and_two_non_fakeable_of_the_same_type_contructor_parameters.cs +++ b/source/developwithpassion.specifications.examples/automatic_sut_creation/with_a_mix_of_fakeable_and_two_non_fakeable_of_the_same_type_contructor_parameters.cs @@ -94,8 +94,8 @@ public class when_adding_two_numbers_with_specific_date_and_time_and_no_default_ It should_have_default_date_time_for_end_time = () => sut.end_date.ShouldEqual(DateTime.MinValue); - It should_have_expected_date_on_property = () => - sut.date_on_property.ShouldEqual(DateTime.MinValue); + It should_have_not_set_the_property_dependency = () => + sut.date_on_property.ShouldEqual(DateTime.MaxValue); It should_return_the_sum = () => result.ShouldEqual(5); @@ -104,7 +104,7 @@ public class when_adding_two_numbers_with_specific_date_and_time_and_no_default_ static DateTime expected_current_date; static DateTime expected_date_on_property; } - + public delegate void SomeDelegate(); public class Calculator @@ -121,6 +121,7 @@ public Calculator(IDbConnection connection, IDataAdapter adapter, DateTime curre this.current_date = current_date; this.end_date = end_date; this.some_delegate = some_delegate; + this.date_on_property = DateTime.MaxValue; } public DateTime current_date { get; private set; } diff --git a/source/developwithpassion.specifications/core/factories/NonCtorDependencySetter.cs b/source/developwithpassion.specifications/core/factories/NonCtorDependencySetter.cs index c1bb9f6..475e436 100644 --- a/source/developwithpassion.specifications/core/factories/NonCtorDependencySetter.cs +++ b/source/developwithpassion.specifications/core/factories/NonCtorDependencySetter.cs @@ -28,7 +28,7 @@ public void update(object item) var accessors_to_update = item.GetType().all_accessors(accessor_flags) .Where( - field => has_no_value_specification.matches(field) || dependency_registry.has_been_provided_an(field.accessor_type)); + field => has_no_value_specification.matches(field) || dependency_registry.has_been_provided_an(field.accessor_type, field.name)); attempt_to_update_all_of_the_accessors(accessors_to_update,item); } diff --git a/source/developwithpassion.specifications/faking/DependencyRegistry.cs b/source/developwithpassion.specifications/faking/DependencyRegistry.cs index d1d5a3a..03e7d10 100644 --- a/source/developwithpassion.specifications/faking/DependencyRegistry.cs +++ b/source/developwithpassion.specifications/faking/DependencyRegistry.cs @@ -9,8 +9,7 @@ namespace developwithpassion.specifications.faking public interface IManageTheDependenciesForASUT :IProvideDependencies { object get_dependency_of(Type dependency_type, string name); - object get_dependency_of(Type dependency_type); - bool has_been_provided_an(Type dependency_type); + bool has_been_provided_an(Type dependency_type, string name); } public class DependenciesRegistry : IManageTheDependenciesForASUT @@ -18,6 +17,7 @@ public class DependenciesRegistry : IManageTheDependenciesForASUT public IDictionary> explicit_dependencies = new Dictionary>(); IResolveADependencyForTheSUT dependency_resolver; IManageFakes fake_gateway; + const string default_dependency = ""; public DependenciesRegistry(IResolveADependencyForTheSUT dependency_resolver,IManageFakes fake_gateway) { @@ -25,19 +25,20 @@ public DependenciesRegistry(IResolveADependencyForTheSUT dependency_resolver,IMa this.fake_gateway = fake_gateway; } - public bool has_been_provided_an(Type dependency_type) + public bool has_been_provided_an(Type dependency_type, string name) { - return explicit_dependencies.ContainsKey(dependency_type); - } + if(explicit_dependencies.ContainsKey(dependency_type) + && (explicit_dependencies[dependency_type].ContainsKey(default_dependency) || explicit_dependencies[dependency_type].ContainsKey(name))) + { + return true; + } - public object get_dependency_of(Type dependency_type) - { - return get_dependency_of(dependency_type, ""); + return false; } public object get_dependency_of(Type dependency_type, string name) { - return (this.explicit_dependencies.ContainsKey(dependency_type) + return (this.has_been_provided_an(dependency_type, name) ? this.get_explicit_dependency(dependency_type, name) : this.dependency_resolver.resolve(dependency_type)); } @@ -49,7 +50,7 @@ public Dependency on() where Dependency : class public Dependency on(Dependency value) { - add_explicit_dependency(typeof(Dependency), value, ""); + add_explicit_dependency(typeof(Dependency), value, default_dependency); return value; } @@ -59,6 +60,11 @@ public Dependency on(Dependency value, string name) return value; } + protected object get_dependency_of(Type dependency_type) + { + return get_dependency_of(dependency_type, default_dependency); + } + private object get_explicit_dependency(Type dependency_type, string name) { if (this.explicit_dependencies[dependency_type].ContainsKey(name)) @@ -66,12 +72,7 @@ private object get_explicit_dependency(Type dependency_type, string name) return this.explicit_dependencies[dependency_type][name]; } - if(this.explicit_dependencies[dependency_type].ContainsKey("")) - { - return this.explicit_dependencies[dependency_type][""]; - } - - return GetDefault(dependency_type); + return this.explicit_dependencies[dependency_type][default_dependency]; } private void add_explicit_dependency(Type dependency_type, object value, string name) @@ -90,14 +91,5 @@ private void add_explicit_dependency(Type dependency_type, object value, string this.explicit_dependencies[dependency_type].Add(name, value); } } - - private static object GetDefault(Type type) - { - if (type.IsValueType) - { - return Activator.CreateInstance(type); - } - return null; - } } } \ No newline at end of file From 748e23310128894945b6821c124af6f5c100334e Mon Sep 17 00:00:00 2001 From: Sean Bennett Date: Sun, 2 Jun 2013 18:27:56 -0600 Subject: [PATCH 6/7] Add tests for dependency registry changes. Remove during_create --- .../DependencyRegistrySpecs.cs | 109 +++++++++++++++++- .../faking/DefaultSUTFactory.cs | 11 +- .../faking/DependencyRegistry.cs | 6 + .../faking/SUTFactory.cs | 1 - 4 files changed, 111 insertions(+), 16 deletions(-) diff --git a/source/developwithpassion.specification.specs/DependencyRegistrySpecs.cs b/source/developwithpassion.specification.specs/DependencyRegistrySpecs.cs index 9956c94..1215850 100644 --- a/source/developwithpassion.specification.specs/DependencyRegistrySpecs.cs +++ b/source/developwithpassion.specification.specs/DependencyRegistrySpecs.cs @@ -33,20 +33,81 @@ public abstract class concern : Observes protected static IDbConnection the_connection; } - public class when_asked_if_it_has_an_explicit_dependency_and_it_has_it_by_name : concern + public class when_creating_sut_with_a_default_dependency_for_idbconnection : concern { Establish c = () => { - dependencies_by_name_for_idbconnection = new Dictionary() { { "", the_connection } }; + dependencies_by_name_for_idbconnection = new Dictionary() { { 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), "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(); + dependencies_by_name_for_idbconnection = new Dictionary() + { + { 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(); + dependencies_by_name_for_idbconnection = new Dictionary() { { "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_default_dependency : concern @@ -55,7 +116,7 @@ public class and_the_dependency_has_been_explicitly_provided_with_a_default : wh { private Establish c = () => { - dependencies_by_name_for_idbconnection = new Dictionary() { { "", the_connection } }; + dependencies_by_name_for_idbconnection = new Dictionary() { { sut.default_dependency_name, the_connection } }; dependencies.Add(typeof(IDbConnection), dependencies_by_name_for_idbconnection); }; @@ -85,6 +146,42 @@ public class and_the_dependency_was_not_explicitly_provided : when_getting_a_def } } + 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() { { "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; + } + + public class and_the_dependency_was_not_explicitly_provided : when_getting_a_default_dependency + { + Establish c = () => + { + dependency_resolver.setup(x => x.resolve(typeof(IDbConnection))).Return(the_connection); + }; + + Because b = () => + 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; + } + } + public class when_storing_default_dependencies : concern { public class and_it_has_not_already_been_stored : when_storing_default_dependencies @@ -98,7 +195,7 @@ public class and_it_has_not_already_been_stored : when_storing_default_dependenc result = sut.on(the_connection); private It should_be_stored_in_the_default_underlying_dependencies = () => - dependencies[typeof(IDbConnection)][""].ShouldEqual(the_connection); + dependencies[typeof(IDbConnection)][sut.default_dependency_name].ShouldEqual(the_connection); It should_return_the_instance_being_registered = () => result.ShouldEqual(the_connection); @@ -118,7 +215,7 @@ public class and_it_has_already_been_stored : when_storing_default_dependencies result = sut.on(the_new_connection); It should_replace_the_existing_default_instance = () => - dependencies[typeof(IDbConnection)][""].ShouldEqual(the_new_connection); + dependencies[typeof(IDbConnection)][sut.default_dependency_name].ShouldEqual(the_new_connection); It should_return_the_instance_being_registered = () => result.ShouldEqual(the_new_connection); @@ -139,7 +236,7 @@ public class and_the_dependency_is_not_being_provided : when_storing_default_dep result = sut.on(); private It should_store_the_item_created_by_the_fake_gateway_as_the_default = () => - dependencies[typeof(IDbConnection)][""].ShouldEqual(the_connection_created_by_the_fake_gateway); + 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; diff --git a/source/developwithpassion.specifications/faking/DefaultSUTFactory.cs b/source/developwithpassion.specifications/faking/DefaultSUTFactory.cs index 279f263..cfc3e32 100755 --- a/source/developwithpassion.specifications/faking/DefaultSUTFactory.cs +++ b/source/developwithpassion.specifications/faking/DefaultSUTFactory.cs @@ -9,20 +9,18 @@ public class DefaultSUTFactory : ICreateAndManageDependenciesFor public Func actual_factory; IManageTheDependenciesForASUT manage_the_dependencies_for_asut; IUpdateNonCtorDependenciesOnAnItem non_ctor_dependency_visitor; - Func, SUT> actual_sut_create_wrapper; - + public DefaultSUTFactory(IManageTheDependenciesForASUT manage_the_dependencies_for_asut, IUpdateNonCtorDependenciesOnAnItem non_ctor_dependency_visitor) { this.actual_factory = create_automatically; - this.actual_sut_create_wrapper = sut_creator => sut_creator(); this.manage_the_dependencies_for_asut = manage_the_dependencies_for_asut; this.non_ctor_dependency_visitor = non_ctor_dependency_visitor; } public SUT create() { - return actual_sut_create_wrapper(() => this.actual_factory()); + return this.actual_factory(); } SUT create_automatically() @@ -41,11 +39,6 @@ public void create_using(Func specific_factory) this.actual_factory = specific_factory; } - public void during_create(Func, SUT> sut_create_wrapper) - { - actual_sut_create_wrapper = sut_create_wrapper; - } - public Dependency on() where Dependency : class { return manage_the_dependencies_for_asut.on(); diff --git a/source/developwithpassion.specifications/faking/DependencyRegistry.cs b/source/developwithpassion.specifications/faking/DependencyRegistry.cs index 03e7d10..59ca341 100644 --- a/source/developwithpassion.specifications/faking/DependencyRegistry.cs +++ b/source/developwithpassion.specifications/faking/DependencyRegistry.cs @@ -10,6 +10,7 @@ public interface IManageTheDependenciesForASUT :IProvideDependencies { object get_dependency_of(Type dependency_type, string name); bool has_been_provided_an(Type dependency_type, string name); + string default_dependency_name { get; } } public class DependenciesRegistry : IManageTheDependenciesForASUT @@ -36,6 +37,11 @@ public bool has_been_provided_an(Type dependency_type, string name) return false; } + public string default_dependency_name + { + get { return default_dependency; } + } + public object get_dependency_of(Type dependency_type, string name) { return (this.has_been_provided_an(dependency_type, name) diff --git a/source/developwithpassion.specifications/faking/SUTFactory.cs b/source/developwithpassion.specifications/faking/SUTFactory.cs index 312b65d..5b127a0 100755 --- a/source/developwithpassion.specifications/faking/SUTFactory.cs +++ b/source/developwithpassion.specifications/faking/SUTFactory.cs @@ -5,6 +5,5 @@ namespace developwithpassion.specifications.faking public interface SUTFactory { void create_using(Func factory); - void during_create(Func, SUT> sut_create_wrapper); } } \ No newline at end of file From 713575e777dcf2d70f0eabe035853c8798c01211 Mon Sep 17 00:00:00 2001 From: Sean Bennett Date: Sun, 2 Jun 2013 18:44:15 -0600 Subject: [PATCH 7/7] Add another example for depends_on by name --- ..._of_the_same_type_contructor_parameters.cs | 90 ++++++++++++++++--- 1 file changed, 79 insertions(+), 11 deletions(-) diff --git a/source/developwithpassion.specifications.examples/automatic_sut_creation/with_a_mix_of_fakeable_and_two_non_fakeable_of_the_same_type_contructor_parameters.cs b/source/developwithpassion.specifications.examples/automatic_sut_creation/with_a_mix_of_fakeable_and_two_non_fakeable_of_the_same_type_contructor_parameters.cs index a78f0f6..c8eaeed 100644 --- a/source/developwithpassion.specifications.examples/automatic_sut_creation/with_a_mix_of_fakeable_and_two_non_fakeable_of_the_same_type_contructor_parameters.cs +++ b/source/developwithpassion.specifications.examples/automatic_sut_creation/with_a_mix_of_fakeable_and_two_non_fakeable_of_the_same_type_contructor_parameters.cs @@ -12,10 +12,10 @@ public class when_adding_two_numbers_with_specific_date_and_times : Observes { - var dateTime = DateTime.Now; - expected_current_date = dateTime; - expected_end_date = dateTime.Add(TimeSpan.FromDays(8)); - expected_date_on_property = dateTime.Add(TimeSpan.FromDays(16)); + var date_time = DateTime.Now; + expected_current_date = date_time; + expected_end_date = date_time.Add(TimeSpan.FromDays(8)); + expected_date_on_property = date_time.Add(TimeSpan.FromDays(16)); depends.on(expected_current_date, "current_date"); depends.on(expected_end_date, "end_date"); depends.on(expected_date_on_property, "date_on_property"); @@ -43,13 +43,13 @@ public class when_adding_two_numbers_with_specific_date_and_times : Observes + public class when_adding_two_numbers_with_specific_date_and_time_for_property_and_parameter : Observes { private Establish c = () => { - var dateTime = DateTime.Now; - expected_current_date = dateTime; - expected_date_on_property = dateTime.Add(TimeSpan.FromDays(16)); + var date_time = DateTime.Now; + expected_current_date = date_time; + expected_date_on_property = date_time.Add(TimeSpan.FromDays(16)); depends.on(expected_current_date, "current_date"); depends.on(expected_date_on_property, "date_on_property"); }; @@ -73,15 +73,83 @@ public class when_adding_two_numbers_with_specific_date_and_time_and_default_dat static DateTime expected_current_date; static DateTime expected_date_on_property; } + + + [Subject(typeof(Calculator))] + public class when_adding_two_numbers_with_specific_date_and_time_for_property_and_default_for_parameters : Observes + { + private Establish c = () => + { + var date_time = DateTime.Now; + expected_date = date_time; + expected_date_on_property = date_time.Add(TimeSpan.FromDays(16)); + depends.on(expected_date); + depends.on(expected_date_on_property, "date_on_property"); + }; + + Because b = () => + result = sut.add(2, 3); + + It should_have_default_date_time_for_current_date = () => + sut.current_date.ShouldEqual(expected_date); + + It should_have_default_date_time_for_end_time = () => + sut.end_date.ShouldEqual(expected_date); + + It should_have_expected_date_on_property = () => + sut.date_on_property.ShouldEqual(expected_date_on_property); + + It should_return_the_sum = () => + result.ShouldEqual(5); + + static int result; + static DateTime expected_date; + static DateTime expected_date_on_property; + } + [Subject(typeof(Calculator))] + public class when_adding_two_numbers_with_specific_date_and_time_for_property_and_parameter_and_other_parameter_default : Observes + { + private Establish c = () => + { + var date_time = DateTime.Now; + expected_current_date = date_time; + expected_end_date = date_time.Add(TimeSpan.FromDays(8)); + expected_date_on_property = date_time.Add(TimeSpan.FromDays(16)); + depends.on(expected_current_date, "current_date"); + depends.on(expected_end_date); + depends.on(expected_date_on_property, "date_on_property"); + }; + + Because b = () => + result = sut.add(2, 3); + + It should_have_expected_current_date = () => + sut.current_date.ShouldEqual(expected_current_date); + + It should_have_expected_end_date = () => + sut.end_date.ShouldEqual(expected_end_date); + + It should_have_expected_date_on_property = () => + sut.date_on_property.ShouldEqual(expected_date_on_property); + + It should_return_the_sum = () => + result.ShouldEqual(5); + + static int result; + static DateTime expected_current_date; + static DateTime expected_date_on_property; + static object expected_end_date; + } + [Subject(typeof(Calculator))] public class when_adding_two_numbers_with_specific_date_and_time_and_no_default_date_and_time : Observes { private Establish c = () => { - var dateTime = DateTime.Now; - expected_current_date = dateTime; - expected_date_on_property = dateTime.Add(TimeSpan.FromDays(16)); + var date_time = DateTime.Now; + expected_current_date = date_time; + expected_date_on_property = date_time.Add(TimeSpan.FromDays(16)); depends.on(expected_current_date, "current_date"); };