Commit 3f7587e

mo khan <mo@mokhan.ca>
2011-03-18 18:54:13
test the AddIncomeCommand.
1 parent 30925fe
product/desktop.ui/bootstrappers/Bootstrapper.cs
@@ -26,6 +26,7 @@ namespace solidware.financials.windows.ui.bootstrappers
             builder.Register(x => shell_window).SingleInstance();
             builder.Register(x => shell_window).As<RegionManager>().SingleInstance();
 
+            builder.RegisterType<IfFamilyMemberIsSelected>().SingleInstance();
             register_needs_startup(builder);
 
             // infrastructure
@@ -52,6 +53,7 @@ namespace solidware.financials.windows.ui.bootstrappers
         {
             builder.RegisterType<ComposeShell>().As<NeedStartup>();
             builder.RegisterType<ConfigureMappings>().As<NeedStartup>();
+
             builder.RegisterType<WireUpSubscribers>().As<NeedStartup>();
         }
 
@@ -82,7 +84,6 @@ namespace solidware.financials.windows.ui.bootstrappers
 
             builder.RegisterType<AddNewIncomeViewModel>();
             builder.RegisterType<AddNewIncomeViewModel.AddIncomeCommand>();
-            builder.RegisterType<IfFamilyMemberIsSelected>().SingleInstance();
 
             builder.RegisterType<TaxSummaryPresenter>();
             
product/desktop.ui/presenters/AddNewIncomeViewModel.cs
@@ -1,12 +1,14 @@
 using System;
 using solidware.financials.infrastructure;
 using solidware.financials.messages;
+using solidware.financials.windows.ui.events;
 
 namespace solidware.financials.windows.ui.presenters
 {
     public class AddNewIncomeViewModel : DialogPresenter
     {
         UICommandBuilder builder;
+        ApplicationState state;
 
         public AddNewIncomeViewModel(UICommandBuilder builder)
         {
@@ -19,23 +21,29 @@ namespace solidware.financials.windows.ui.presenters
             Cancel = builder.build<CancelCommand>(this);
         }
 
-        public decimal amount { get; set; }
+        public virtual decimal amount { get; set; }
         public IObservableCommand Add { get; set; }
         public IObservableCommand Cancel { get; set; }
-        public Action close { get; set; }
+        public virtual Action close { get; set; }
 
         public class AddIncomeCommand : UICommand<AddNewIncomeViewModel>
         {
             ServiceBus bus;
+            ApplicationState applicationState;
 
-            public AddIncomeCommand(ServiceBus bus)
+            public AddIncomeCommand(ServiceBus bus, ApplicationState applicationState)
             {
                 this.bus = bus;
+                this.applicationState = applicationState;
             }
 
             public override void run(AddNewIncomeViewModel presenter)
             {
-                bus.publish<AddIncomeCommandMessage>(x => { x.amount = presenter.amount; });
+                bus.publish(new AddIncomeCommandMessage
+                            {
+                                Amount = presenter.amount,
+                                PersonId = applicationState.PullOut<SelectedFamilyMember>().id
+                            });
                 presenter.close();
             }
         }
product/desktop.ui/presenters/TaxSummaryPresenter.cs
@@ -29,7 +29,7 @@ namespace solidware.financials.windows.ui.presenters
 
         public void notify(AddIncomeCommandMessage message)
         {
-            Selected.AddIncome(message.amount);
+            Selected.AddIncome(message.Amount);
         }
 
         public void notify(SelectedFamilyMember message)
product/desktop.ui/Properties/AssemblyInfo.cs
@@ -49,5 +49,5 @@ using System.Windows;
 // You can specify all the values or you can default the Build and Revision Numbers 
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("2011.3.17.238")]
-[assembly: AssemblyFileVersion("2011.3.17.238")]
+[assembly: AssemblyVersion("2011.3.18.1253")]
+[assembly: AssemblyFileVersion("2011.3.18.1253")]
product/desktop.ui/ApplicationState.cs
@@ -0,0 +1,8 @@
+namespace solidware.financials.windows.ui
+{
+    public interface ApplicationState
+    {
+        Token PullOut<Token>();
+        void PushIn<Token>(Token token);
+    }
+}
\ No newline at end of file
product/desktop.ui/InMemoryApplicationState.cs
@@ -0,0 +1,21 @@
+using System;
+using System.Collections.Generic;
+using gorilla.utility;
+
+namespace solidware.financials.windows.ui
+{
+    public class InMemoryApplicationState : ApplicationState
+    {
+        IDictionary<Type, object> database = new Dictionary<Type, object>();
+
+        public Token PullOut<Token>()
+        {
+            return database[typeof (Token)].downcast_to<Token>();
+        }
+
+        public void PushIn<Token>(Token token)
+        {
+            database[typeof (Token)] = token;
+        }
+    }
+}
\ No newline at end of file
product/desktop.ui/solidware.financials.csproj
@@ -99,6 +99,7 @@
     <Compile Include="events\SelectedFamilyMember.cs" />
     <Compile Include="events\UpdateOnLongRunningProcess.cs" />
     <Compile Include="handlers\PublishEventHandler.cs" />
+    <Compile Include="InMemoryApplicationState.cs" />
     <Compile Include="IObservableCommand.cs" />
     <Compile Include="model\FederalTaxes.cs" />
     <Compile Include="model\TaxesForIndividual.cs" />
@@ -107,6 +108,7 @@
     <Compile Include="PresenterFactory.cs" />
     <Compile Include="presenters\AddFamilyMemberPresenter.cs" />
     <Compile Include="presenters\AddNewIncomeViewModel.cs" />
+    <Compile Include="ApplicationState.cs" />
     <Compile Include="presenters\DisplayCanadianTaxInformationViewModel.cs" />
     <Compile Include="model\PersonDetails.cs" />
     <Compile Include="presenters\IfFamilyMemberIsSelected.cs" />
product/messages/AddIncomeCommandMessage.cs
@@ -1,9 +1,12 @@
-using solidware.financials.infrastructure.eventing;
+using System;
+using gorilla.utility;
+using solidware.financials.infrastructure.eventing;
 
 namespace solidware.financials.messages
 {
-    public class AddIncomeCommandMessage : Event
+    public class AddIncomeCommandMessage : ValueType<AddIncomeCommandMessage>, Event
     {
-        public decimal amount { get; set; }
+        public decimal Amount { get; set; }
+        public Guid PersonId { get; set; }
     }
 }
\ No newline at end of file
product/messages/messages.csproj
@@ -31,6 +31,9 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="gorilla.utility">
+      <HintPath>..\..\thirdparty\commons\gorilla.utility.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Xml.Linq" />
product/specs/unit/ui/presenters/AddNewIncomeViewModelSpecs.cs
@@ -0,0 +1,114 @@
+using System;
+using Machine.Specifications;
+using Rhino.Mocks;
+using solidware.financials.infrastructure;
+using solidware.financials.messages;
+using solidware.financials.windows.ui;
+using solidware.financials.windows.ui.events;
+using solidware.financials.windows.ui.presenters;
+
+namespace specs.unit.ui.presenters
+{
+    public class AddNewIncomeViewModelSpecs
+    {
+        public class concern
+        {
+            Establish context = () =>
+            {
+                command_builder = Create.dependency<UICommandBuilder>();
+
+                sut = new AddNewIncomeViewModel(command_builder);
+            };
+
+            static protected AddNewIncomeViewModel sut;
+            static protected UICommandBuilder command_builder;
+        }
+
+        public class when_clicking_the_cancel_button : concern
+        {
+            It should_invoke_the_cancel_command = () =>
+            {
+                cancel_command.received(x => x.Execute(sut));
+            };
+
+            Establish context = () =>
+            {
+                cancel_command = Create.an<IObservableCommand>();
+                command_builder.Stub(x => x.build<CancelCommand>(sut)).Return(cancel_command);
+            };
+
+            Because of = () =>
+            {
+                sut.present();
+                sut.Cancel.Execute(sut);
+            };
+
+            static IObservableCommand cancel_command;
+        }
+
+        public class when_clicking_the_add_button : concern
+        {
+            It should_invoke_the_add_command = () =>
+            {
+                add_command.received(x => x.Execute(sut));
+            };
+
+            Establish context = () =>
+            {
+                add_command = Create.an<IObservableCommand>();
+                command_builder.Stub(x => x.build<AddNewIncomeViewModel.AddIncomeCommand, IfFamilyMemberIsSelected>(sut)).Return(add_command);
+            };
+
+            Because of = () =>
+            {
+                sut.present();
+                sut.Add.Execute(sut);
+            };
+
+            static IObservableCommand add_command;
+        }
+
+        public class when_the_add_button_is_pressed
+        {
+            It should_push_a_message_to_the_service_bus = () =>
+            {
+                bus.received(x => x.publish(new AddIncomeCommandMessage
+                                            {
+                                                Amount = amount,
+                                                PersonId = personId
+                                            }));
+            };
+
+            It should_close_the_dialog = () =>
+            {
+                closed.ShouldBeTrue();
+            };
+
+            Establish context = () =>
+            {
+                bus = Create.dependency<ServiceBus>();
+                state = Create.an<ApplicationState>();
+                var person = new SelectedFamilyMember {id = personId};
+                state.Stub(x => x.PullOut<SelectedFamilyMember>()).Return(person);
+                presenter = Create.an<AddNewIncomeViewModel>();
+                presenter.Stub(x => x.amount).Return(amount);
+                presenter.Stub(x => x.close).Return(() => { closed = true; });
+
+                sut = new AddNewIncomeViewModel.AddIncomeCommand(bus, state);
+            };
+
+            Because of = () =>
+            {
+                sut.run(presenter);
+            };
+
+            static AddNewIncomeViewModel.AddIncomeCommand sut;
+            static ServiceBus bus;
+            static AddNewIncomeViewModel presenter;
+            static decimal amount = 1000m;
+            static ApplicationState state;
+            static Guid personId = Guid.NewGuid();
+            static bool closed;
+        }
+    }
+}
\ No newline at end of file
product/specs/unit/ui/InMemoryApplicationStateSpecs.cs
@@ -0,0 +1,34 @@
+using Machine.Specifications;
+using solidware.financials.windows.ui;
+
+namespace specs.unit.ui
+{
+    public class InMemoryApplicationStateSpecs
+    {
+        public class when_storing_something_in_memory
+        {
+            It should_be_easy_to_pull_it_out_of_memory = () =>
+            {
+                result.ShouldEqual(token);
+            };
+
+            Establish context = () =>
+            {
+                token = new TestToken();
+                sut = new InMemoryApplicationState();
+            };
+
+            Because of = () =>
+            {
+                sut.PushIn(token);
+                result = sut.PullOut<TestToken>();
+            };
+
+            static ApplicationState sut;
+            static TestToken token;
+            static TestToken result;
+        }
+
+        class TestToken {}
+    }
+}
\ No newline at end of file
product/specs/specs.csproj
@@ -33,6 +33,7 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="gorilla.utility, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL" />
     <Reference Include="Machine.Specifications">
       <HintPath>..\..\thirdparty\mspec\Machine.Specifications.dll</HintPath>
     </Reference>
@@ -53,12 +54,22 @@
     <Compile Include="Mocking.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Create.cs" />
+    <Compile Include="unit\ui\InMemoryApplicationStateSpecs.cs" />
+    <Compile Include="unit\ui\presenters\AddNewIncomeViewModelSpecs.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\desktop.ui\solidware.financials.csproj">
       <Project>{1E07A7D1-661C-48A2-B67B-180C6107CC92}</Project>
       <Name>solidware.financials</Name>
     </ProjectReference>
+    <ProjectReference Include="..\infrastructure\infrastructure.csproj">
+      <Project>{16D56F38-F4B0-4134-907A-837E4C62C7B7}</Project>
+      <Name>infrastructure</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\messages\messages.csproj">
+      <Project>{C3DF753C-7BB7-48E0-B87D-D37ED47EDF92}</Project>
+      <Name>messages</Name>
+    </ProjectReference>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
thirdparty/commons/gorilla.infrastructure.dll
Binary file
thirdparty/commons/gorilla.utility.dll
Binary file
.gitignore
@@ -7,3 +7,4 @@ _ReSharper.*
 _build.log
 tmp
 local.properties.xml
+%APPDATA%