main
1using System;
2using System.Linq;
3using gorilla.infrastructure.threading;
4using Machine.Specifications;
5using Rhino.Mocks;
6using solidware.financials.infrastructure;
7using solidware.financials.messages;
8using solidware.financials.windows.ui;
9using solidware.financials.windows.ui.presenters;
10using solidware.financials.windows.ui.views.dialogs;
11
12namespace specs.unit.ui.presenters
13{
14 public class StockWatchPresenterSpecs
15 {
16 [Subject(typeof (StockWatchPresenter))]
17 public abstract class concern
18 {
19 Establish context = () =>
20 {
21 builder = Create.dependency<UICommandBuilder>();
22 timer = Create.dependency<Timer>();
23 sut = new StockWatchPresenter(builder, timer);
24 };
25
26 static protected StockWatchPresenter sut;
27 static protected UICommandBuilder builder;
28 static protected Timer timer;
29 }
30
31 public class when_loading_the_region : concern
32 {
33 Establish context = () =>
34 {
35 add_command = Create.an<ObservableCommand>();
36 builder.Stub(x => x.build<StockWatchPresenter.AddSymbolCommand>(sut)).Return(add_command);
37 };
38
39 Because of = () =>
40 {
41 sut.present();
42 };
43
44 It should_start_a_timer_to_periodically_fetch_stock_prices = () =>
45 {
46 timer.received(x => x.start_notifying(sut, new TimeSpan(0, 1, 0)));
47 };
48
49 It should_build_the_add_symbol_command = () =>
50 {
51 sut.AddSymbol.should_be_equal_to(add_command);
52 };
53
54 static ObservableCommand add_command;
55 }
56
57 public class when_one_minute_has_elapsed : concern
58 {
59 Establish context = () =>
60 {
61 refresh_command = Create.an<ObservableCommand>();
62 builder.Stub(x => x.build<StockWatchPresenter.RefreshStockPricesCommand>(sut)).Return(refresh_command);
63 };
64
65 Because of = () =>
66 {
67 sut.present();
68 sut.notify();
69 };
70
71 It should_refresh_the_stock_prices = () =>
72 {
73 refresh_command.received(x => x.Execute(sut));
74 };
75
76 static ObservableCommand refresh_command;
77 }
78
79 public class when_a_stock_price_changes : concern
80 {
81 Establish context = () =>
82 {
83 sut.Stocks.Add(new StockViewModel(symbol:"ARX.TO", builder: null)
84 {
85 Price = new Money(20.00m).ToObservable(),
86 });
87 };
88
89 Because of = () =>
90 {
91 sut.notify(new CurrentStockPrice {Symbol = "ARX.TO", Price = 25.50m});
92 };
93
94 It should_display_the_new_price = () =>
95 {
96 sut.Stocks.Last().Price.Value.should_be_equal_to(new Money(25.50m));
97 };
98 }
99
100 public class when_starting_to_watch_a_new_symbol : concern
101 {
102 Because of = () =>
103 {
104 sut.notify(new StartWatchingSymbol {Symbol = "TD.TO",});
105 };
106
107 It should_add_the_new_symbol_to_the_list_of_stocks_to_watch = () =>
108 {
109 sut.Stocks.should_contain(x => x.Symbol.Equals("TD.TO"));
110 };
111 }
112
113 public class AddSymbolCommandSpecs
114 {
115 public class when_a_user_wants_to_watch_a_new_symbol
116 {
117 Establish context = () =>
118 {
119 presenter = Create.an<StockWatchPresenter>();
120 controller = Create.dependency<DialogLauncher>();
121 sut = new StockWatchPresenter.AddSymbolCommand(controller);
122 };
123
124 Because of = () =>
125 {
126 sut.run(presenter);
127 };
128
129 It should_launch_the_add_stock_symbol_dialog = () =>
130 {
131 controller.received(x => x.launch<AddNewStockSymbolPresenter, AddNewStockSymbolDialog>());
132 };
133
134 static DialogLauncher controller;
135 static StockWatchPresenter.AddSymbolCommand sut;
136 static StockWatchPresenter presenter;
137 }
138 }
139
140 public class RefreshStockPricesCommandSpecs
141 {
142 public class concern
143 {
144 Establish context = () =>
145 {
146 bus = Create.dependency<ServiceBus>();
147
148 sut = new StockWatchPresenter.RefreshStockPricesCommand(bus);
149 };
150
151 static protected ServiceBus bus;
152 static protected StockWatchPresenter.RefreshStockPricesCommand sut;
153 }
154
155 public class when_refreshing_the_stock_prices : concern
156 {
157 Establish context = () =>
158 {
159 presenter = Create.an<StockWatchPresenter>();
160 presenter.is_told_to(x => x.Stocks).it_will_return(new StockViewModel (symbol : "ARX.TO", builder: null));
161 };
162
163 Because of = () =>
164 {
165 sut.run(presenter);
166 };
167
168 It should_fetch_the_stock_price_for_each_symbol = () =>
169 {
170 bus.was_told_to(x => x.publish(new StockPriceRequestQuery {Symbol = "ARX.TO"}));
171 };
172
173 static StockWatchPresenter presenter;
174 }
175 }
176 }
177}