organized and cleaned up tests

This commit is contained in:
mgroves 2011-03-30 22:34:11 -04:00
parent e0df956f7c
commit e145282a52
37 changed files with 740 additions and 479 deletions

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Android.Runtime;
using Android.Util;
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Core.StockData;

View file

@ -5,6 +5,7 @@ using MonoStockPortfolio.Framework;
namespace MonoStockPortfolio.Tests.Framework
{
[Tags("UnitTest")]
public class When_validating_forms_with_validation_errors
{
static FormValidator _validator;

View file

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Machine.Specifications;
namespace MonoStockPortfolio.Tests
{
[Tags("MetaTest")]
public class MetaTests
{
static Type[] _allTypes;
static IList<Type> _allTestTypes;
Establish context = () =>
{
_allTypes = Assembly.GetAssembly(typeof (MetaTests)).GetTypes();
};
Because of = () =>
{
_allTestTypes = _allTypes.Where(IsTestFixture).ToList();
};
It should_have_found_all_the_test_fixtures = () =>
_allTestTypes.ShouldNotBeEmpty();
It should_have_tags_on_every_test_fixture = () =>
_allTestTypes.All(HasProperTags).ShouldBeTrue();
It should_output_all_test_classes_that_arent_tagged_properly = () =>
{
var sb = new StringBuilder();
var missingtags = _allTestTypes.Where(t => !HasProperTags(t));
if (missingtags.Any())
{
foreach (var missingtag in missingtags)
{
sb.AppendFormat("Test class missing tag: {0}", missingtag.Name)
.AppendLine();
}
throw new CustomAttributeFormatException(sb.ToString());
}
};
static bool HasProperTags(Type type)
{
var attrs = Attribute.GetCustomAttributes(type);
var tagAttrs = attrs.Where(a => a.GetType() == typeof (TagsAttribute));
if(!tagAttrs.Any())
{
return false;
}
return tagAttrs.Select(a => (TagsAttribute) a)
.All(ta => ta.Tags
.All(t => t.Name == "UnitTest"
|| t.Name == "IntegrationTest"
|| t.Name == "MetaTest"));
}
static bool IsTestFixture(Type type)
{
var privateFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Static);
foreach (var privateField in privateFields)
{
Console.WriteLine(privateField.GetType());
}
return privateFields.Any(f => f.FieldType == typeof (It));
}
}
}

View file

@ -60,12 +60,39 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Framework\When_validating_forms_with_validation_errors.cs" />
<Compile Include="MetaTests.cs" />
<Compile Include="Presenters\Given_an_initialized_Config_Presenter.cs" />
<Compile Include="Presenters\EditPortfolioTests.cs" />
<Compile Include="Presenters\EditPositionTests.cs" />
<Compile Include="Presenters\MainPresenterTests.cs" />
<Compile Include="Presenters\PortfolioPresenterTests.cs" />
<Compile Include="Presenters\When_done_initializing_a_Portfolio_Presenter.cs" />
<Compile Include="Presenters\When_initialize_the_config_presenter.cs" />
<Compile Include="Presenters\When_initializing_the_edit_portfolio_presenter_with_an_id.cs" />
<Compile Include="Presenters\When_initializing_the_edit_portfolio_presenter_with_no_id.cs" />
<Compile Include="Presenters\When_initializing_the_edit_position_presenter_with_an_id.cs" />
<Compile Include="Presenters\When_initializing_the_edit_position_presenter_with_no_id.cs" />
<Compile Include="Presenters\When_initializing_the_Main_Presenter.cs" />
<Compile Include="Presenters\When_the_user_selects_delete_context_option.cs" />
<Compile Include="Presenters\When_the_user_selects_edit_context_option.cs" />
<Compile Include="Presenters\When_the_user_tries_to_save_a_new_portfolio_with_a_blank_name.cs" />
<Compile Include="Presenters\When_the_user_tries_to_save_a_portfolio_with_a_duplicated_name.cs" />
<Compile Include="Presenters\When_the_user_wants_to_add_a_new_portfolio.cs" />
<Compile Include="Presenters\When_the_user_wants_to_add_a_new_position.cs" />
<Compile Include="Presenters\When_the_user_wants_to_configure_the_display_fields.cs" />
<Compile Include="Presenters\When_the_user_wants_to_delete_a_portfolio.cs" />
<Compile Include="Presenters\When_the_user_wants_to_edit_a_portfolio.cs" />
<Compile Include="Presenters\When_the_user_wants_to_exit_the_app.cs" />
<Compile Include="Presenters\When_the_user_wants_to_refresh_the_positions.cs" />
<Compile Include="Presenters\When_the_user_wants_to_save_an_invalid_position.cs" />
<Compile Include="Presenters\When_the_user_wants_to_save_an_invalid_position_with_blank_fields.cs" />
<Compile Include="Presenters\When_the_user_wants_to_save_a_valid_portfolio.cs" />
<Compile Include="Presenters\When_the_user_wants_to_save_a_valid_position.cs" />
<Compile Include="Presenters\When_the_user_wants_to_save_configuration.cs" />
<Compile Include="Presenters\When_the_user_wants_to_see_the_context_menu.cs" />
<Compile Include="Presenters\When_the_user_wants_to_view_a_portfolio.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\When_using_the_Yahoo_stock_data_service_to_validate_tickers.cs" />
<Compile Include="Services\YahooStockDataServiceTests.cs" />
</ItemGroup>
<ItemGroup>

View file

@ -1,11 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using Machine.Specifications;
using MonoStockPortfolio.Activites.EditPortfolioScreen;
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
using Telerik.JustMock.Helpers;
namespace MonoStockPortfolio.Tests.Presenters
{
@ -26,88 +23,4 @@ namespace MonoStockPortfolio.Tests.Presenters
_presenter = new EditPortfolioPresenter(_mockPortfolioRepository);
};
}
public class When_initializing_the_edit_portfolio_presenter_with_no_id : EditPortfolioTests
{
Because of = () =>
{
_presenter.Initialize(_mockEditPortfolioView, null);
};
It should_set_the_title_to_Add_New_Portfolio = () =>
Mock.Assert(() => _mockEditPortfolioView.SetTitle("Add New Portfolio"), Occurs.Exactly(1));
It shouldnt_prepopulate_the_form_with_anything = () =>
Mock.Assert(() => _mockEditPortfolioView.PopulateForm(Arg.IsAny<Portfolio>()), Occurs.Never());
}
public class When_initializing_the_edit_portfolio_presenter_with_an_id : EditPortfolioTests
{
Because of = () =>
{
_presenter.Initialize(_mockEditPortfolioView, 999);
};
It should_set_the_title_to_Edit_Portfolio = () =>
Mock.Assert(() => _mockEditPortfolioView.SetTitle("Edit Portfolio"), Occurs.Exactly(1));
It should_prepopulate_the_form_with_a_portfolio_name = () =>
Mock.Assert(() => _mockEditPortfolioView.PopulateForm(Arg.Matches<Portfolio>(x => x.Name == "Testing Portfolio!")), Occurs.Exactly(1));
}
public class When_the_user_wants_to_save_a_valid_portfolio : EditPortfolioTests
{
Establish context = () =>
{
_presenter.Initialize(_mockEditPortfolioView, null);
};
Because of = () =>
{
_presenter.SavePortfolio(new Portfolio(999) {Name = "Whatever Portfolio"});
};
It should_use_the_repository_to_save_the_portfolio = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePortfolio(Arg.Matches<Portfolio>(x => x.ID == 999 && x.Name == "Whatever Portfolio")), Occurs.Exactly(1));
It should_tell_the_view_to_show_a_nice_saved_message = () =>
Mock.Assert(() => _mockEditPortfolioView.ShowSaveSuccessMessage("You saved: Whatever Portfolio"), Occurs.Exactly(1));
It should_tell_the_view_to_go_back_to_the_main_activity = () =>
Mock.Assert(() => _mockEditPortfolioView.GoBackToMainActivity(), Occurs.Exactly(1));
}
public class When_the_user_tries_to_save_a_new_portfolio_with_a_blank_name : EditPortfolioTests
{
Establish context = () =>
{
_presenter.Initialize(_mockEditPortfolioView);
};
Because of = () =>
{
_presenter.SavePortfolio(new Portfolio {Name = ""});
};
It should_return_1_validation_error = () =>
Mock.Assert(() => _mockEditPortfolioView.ShowValidationErrors(Arg.Matches<IEnumerable<string>>(x => x.Count() == 1)), Occurs.Exactly(1));
It should_return_a_nice_required_validation_error_message = () =>
Mock.Assert(() => _mockEditPortfolioView.ShowValidationErrors(Arg.Matches<IEnumerable<string>>(x => x.Single() == "Please enter a portfolio name")), Occurs.Exactly(1));
}
public class When_the_user_tries_to_save_a_portfolio_with_a_duplicated_name : EditPortfolioTests
{
Establish context = () =>
{
_presenter.Initialize(_mockEditPortfolioView);
};
Because of = () =>
{
Mock.Arrange(() => _mockPortfolioRepository.GetPortfolioByName(Arg.AnyString)).Returns(
new Portfolio(998) {Name = "Some Name"});
_presenter.SavePortfolio(new Portfolio {Name = "Some Name"});
};
It should_return_1_validation_error = () =>
Mock.Assert(() => _mockEditPortfolioView.ShowValidationErrors(Arg.Matches<IEnumerable<string>>(x => x.Count() == 1)), Occurs.Exactly(1));
It should_return_a_nice_duplication_error_message = () =>
Mock.Assert(() => _mockEditPortfolioView.ShowValidationErrors(Arg.Matches<IEnumerable<string>>(x => x.Single() == "Portfolio name is already taken")), Occurs.Exactly(1));
}
}

View file

@ -1,14 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Machine.Specifications;
using MonoStockPortfolio.Activites.EditPositionScreen;
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Core.StockData;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
using Telerik.JustMock.Helpers;
namespace MonoStockPortfolio.Tests.Presenters
{
@ -29,137 +23,4 @@ namespace MonoStockPortfolio.Tests.Presenters
};
}
public class When_initializing_the_edit_position_presenter_with_no_id : EditPositionTests
{
Because of = () =>
{
_presenter.Initialize(_mockView, 1);
};
It should_set_the_title_to_Add_Position = () =>
Mock.Assert(() => _mockView.SetTitle("Add Position"), Occurs.Exactly(1));
It shouldnt_prepopulate_the_form_with_anything = () =>
Mock.Assert(() => _mockView.PopulateForm(Arg.IsAny<Position>()),Occurs.Never());
}
public class When_initializing_the_edit_position_presenter_with_an_id : EditPositionTests
{
Establish context = () =>
{
var fakePosition = new Position(999) { ContainingPortfolioID = 1, PricePerShare = 5.99M, Shares = 50M, Ticker = "FAKE" };
Mock.Arrange(() => _mockPortfolioRepository.GetPositionById(999)).Returns(fakePosition);
};
Because of = () =>
{
_presenter.Initialize(_mockView, 1, 999);
};
It should_set_the_title_to_Edit_Position = () =>
Mock.Assert(() => _mockView.SetTitle("Edit Position"), Occurs.Exactly(1));
It should_prepopulate_the_PricePerShare_on_the_form = () =>
Mock.Assert(() => _mockView.PopulateForm(Arg.Matches<Position>(p => p.PricePerShare == 5.99M)), Occurs.Exactly(1));
It should_prepopulate_the_Shares_on_the_form = () =>
Mock.Assert(() => _mockView.PopulateForm(Arg.Matches<Position>(p => p.Shares == 50M)), Occurs.Exactly(1));
It should_prepopulate_the_Ticker_on_the_form = () =>
Mock.Assert(() => _mockView.PopulateForm(Arg.Matches<Position>(p => p.Ticker == "FAKE")), Occurs.Exactly(1));
}
public class When_the_user_wants_to_save_a_valid_position : EditPositionTests
{
Establish context = () =>
{
_presenter.Initialize(_mockView, 1);
Mock.Arrange(() => _mockStockService.IsValidTicker(Arg.AnyString)).Returns(true);
};
Because of = () =>
{
var fakeInputModel = new PositionInputModel {PriceText = "2.34", SharesText = "671", TickerText = "LOL"};
_presenter.Save(fakeInputModel);
};
It should_save_a_position_with_the_portfolio_repository = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.IsAny<Position>()), Occurs.Exactly(1));
It should_save_a_position_with_the_correct_Price = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.Matches<Position>(p => p.PricePerShare == 2.34M)), Occurs.Exactly(1));
It should_save_a_position_with_the_correct_Shares = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.Matches<Position>(p => p.Shares == 671M)), Occurs.Exactly(1));
It should_save_a_position_with_the_correct_Ticker = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.Matches<Position>(p => p.Ticker == "LOL")), Occurs.Exactly(1));
It should_save_a_position_with_the_correct_Containing_Portfolio_ID = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.Matches<Position>(p => p.ContainingPortfolioID == 1)), Occurs.Exactly(1));
It should_tell_the_view_to_go_back_to_the_main_activity = () =>
Mock.Assert(() => _mockView.GoBackToMainActivity(), Occurs.Exactly(1));
}
public class When_the_user_wants_to_save_an_invalid_position : EditPositionTests
{
Establish context = () =>
{
_presenter.Initialize(_mockView, 1);
Mock.Arrange(() => _mockStockService.IsValidTicker(Arg.AnyString)).Returns(false);
};
Because of = () =>
{
var fakeInputModel = new PositionInputModel {PriceText = "cows", SharesText = "WALRUS!!", TickerText = "fail"};
_presenter.Save(fakeInputModel);
};
It should_not_try_to_save_the_portfolio = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.IsAny<Position>()), Occurs.Never());
It should_send_the_validation_errors_to_the_view = () =>
Mock.Assert(() => _mockView.ShowErrorMessages(Arg.IsAny<IList<string>>()), Occurs.Exactly(1));
It should_send_an_invalid_ticker_error_to_the_view = () =>
MockAssertPositionMatches(x => x.Any(p => p == "Invalid Ticker Name"));
It should_send_an_invalid_shares_number_error_to_the_view = () =>
MockAssertPositionMatches(x => x.Any(p => p == "Please enter a valid, positive number of shares"));
It should_send_an_invalid_price_per_share_error_to_the_view = () =>
MockAssertPositionMatches(x => x.Any(p => p == "Please enter a valid, positive price per share"));
It should_not_tell_the_view_to_go_back_to_the_main_activity = () =>
Mock.Assert(() => _mockView.GoBackToMainActivity(), Occurs.Never());
private static void MockAssertPositionMatches(Expression<Predicate<IList<string>>> match)
{
Mock.Assert(() => _mockView.ShowErrorMessages(Arg.Matches(match)), Occurs.Exactly(1));
}
}
public class When_the_user_wants_to_save_an_invalid_position_with_blank_fields : EditPositionTests
{
Establish context = () =>
{
_presenter.Initialize(_mockView, 1);
Mock.Arrange(() => _mockStockService.IsValidTicker(Arg.AnyString)).Returns(false);
};
Because of = () =>
{
var fakeInputModel = new PositionInputModel { PriceText = "", SharesText = "", TickerText = "" };
_presenter.Save(fakeInputModel);
};
It should_not_try_to_save_the_portfolio = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.IsAny<Position>()), Occurs.Never());
It should_send_the_validation_errors_to_the_view = () =>
Mock.Assert(() => _mockView.ShowErrorMessages(Arg.IsAny<IList<string>>()), Occurs.Exactly(1));
It should_send_an_invalid_ticker_error_to_the_view = () =>
MockPositionMatches(x => x.Any(p => p == "Please enter a ticker"));
It should_send_an_invalid_shares_number_error_to_the_view = () =>
MockPositionMatches(x => x.Any(p => p == "Please enter a valid, positive number of shares"));
It should_send_an_invalid_price_per_share_error_to_the_view = () =>
MockPositionMatches(x => x.Any(p => p == "Please enter a valid, positive price per share"));
It should_not_tell_the_view_to_go_back_to_the_main_activity = () =>
Mock.Assert(() => _mockView.GoBackToMainActivity(), Occurs.Never());
private static void MockPositionMatches(Expression<Predicate<IList<string>>> match)
{
Mock.Assert(() => _mockView.ShowErrorMessages(Arg.Matches(match)), Occurs.Exactly(1));
}
}
}

View file

@ -1,11 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using Machine.Specifications;
using MonoStockPortfolio.Activites.ConfigScreen;
using MonoStockPortfolio.Core.Config;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
using MonoStockPortfolio.Core;
namespace MonoStockPortfolio.Tests.Presenters
{
@ -20,75 +18,14 @@ namespace MonoStockPortfolio.Tests.Presenters
_configView = Mock.Create<IConfigView>();
_configRepository = Mock.Create<IConfigRepository>();
Mock.Arrange(() => _configRepository.GetStockItems()).Returns(new List<StockDataItem>
{
StockDataItem.GainLoss,
StockDataItem.Change
});
Mock.Arrange(() => _configRepository.GetStockItems())
.Returns(new List<StockDataItem>
{
StockDataItem.GainLoss,
StockDataItem.Change
});
_presenter = new ConfigPresenter(_configRepository);
};
}
public class When_initialize_the_config_presenter : Given_a_Config_Presenter
{
static List<StockDataItem> _allStockDataItems;
Establish context = () =>
{
_allStockDataItems = StockDataItem.Volume.GetValues<StockDataItem>().ToList();
};
Because of = () =>
{
_presenter.Initialize(_configView);
};
It should_send_two_checked_items = () =>
Mock.Assert(() =>
_configView.PrepopulateConfiguration(
Arg.IsAny<IList<StockDataItem>>(),
Arg.Matches<IEnumerable<StockDataItem>>(i => i.Count() == 2)),
Occurs.Exactly(1));
It should_send_GainLoss_as_a_checked_item = () =>
Mock.Assert(() =>
_configView.PrepopulateConfiguration(
Arg.IsAny<IList<StockDataItem>>(),
Arg.Matches<IEnumerable<StockDataItem>>(i => i.Any(p => p == StockDataItem.GainLoss))),
Occurs.Exactly(1));
It should_send_Change_as_a_checked_item = () =>
Mock.Assert(() =>
_configView.PrepopulateConfiguration(
Arg.IsAny<IList<StockDataItem>>(),
Arg.Matches<IEnumerable<StockDataItem>>(i => i.Any(p => p == StockDataItem.Change))),
Occurs.Exactly(1));
It should_send_an_enumerated_list_of_all_stock_items = () =>
Mock.Assert(() =>
_configView.PrepopulateConfiguration(
Arg.Matches<IList<StockDataItem>>(i => i.Count == _allStockDataItems.Count),
Arg.IsAny<IEnumerable<StockDataItem>>()),
Occurs.Exactly(1));
}
public class When_the_user_wants_to_save_configuration : Given_a_Config_Presenter
{
Establish context = () =>
{
_presenter.Initialize(_configView);
};
Because of = () =>
{
_presenter.SaveConfig(new List<StockDataItem> {StockDataItem.Ticker, StockDataItem.Time});
};
It should_use_the_repo_to_update_to_2_stock_items = () =>
Mock.Assert(() => _configRepository.UpdateStockItems(Arg.Matches<List<StockDataItem>>(i => i.Count == 2)));
It should_use_the_repo_to_update_stock_items_with_Time = () =>
Mock.Assert(() => _configRepository.UpdateStockItems(Arg.Matches<List<StockDataItem>>(i => i.Any(s => s == StockDataItem.Time))));
It should_use_the_repo_to_update_stock_items_with_Ticker = () =>
Mock.Assert(() => _configRepository.UpdateStockItems(Arg.Matches<List<StockDataItem>>(i => i.Any(s => s == StockDataItem.Ticker))));
}
}

View file

@ -1,13 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using Machine.Specifications;
using MonoStockPortfolio.Activites.MainScreen;
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
using Telerik.JustMock.Helpers;
namespace MonoStockPortfolio.Tests.Activities
namespace MonoStockPortfolio.Tests.Presenters
{
public class Given_an_initialized_Main_Presenter
{
@ -19,106 +17,20 @@ namespace MonoStockPortfolio.Tests.Activities
protected static Portfolio _portfolio2;
Establish context = () =>
{
_portfolio1 = new Portfolio(555) { Name = "portfolio1" };
_portfolio2 = new Portfolio(777) { Name = "portfolio2" };
_portfolioList = new List<Portfolio>();
_portfolioList.Add(_portfolio1);
_portfolioList.Add(_portfolio2);
{
_portfolio1 = new Portfolio(555) {Name = "portfolio1"};
_portfolio2 = new Portfolio(777) {Name = "portfolio2"};
_portfolioList = new List<Portfolio>();
_portfolioList.Add(_portfolio1);
_portfolioList.Add(_portfolio2);
_mockPortfolioRepository = Mock.Create<IPortfolioRepository>();
Mock.Arrange(() => _mockPortfolioRepository.GetAllPortfolios()).Returns(_portfolioList);
Mock.Arrange(() => _mockPortfolioRepository.GetPortfolioByName("portfolio1")).Returns(_portfolio1);
_mockView = Mock.Create<IMainView>();
_mockPortfolioRepository = Mock.Create<IPortfolioRepository>();
Mock.Arrange(() => _mockPortfolioRepository.GetAllPortfolios()).Returns(_portfolioList);
Mock.Arrange(() => _mockPortfolioRepository.GetPortfolioByName("portfolio1")).Returns(_portfolio1);
_mockView = Mock.Create<IMainView>();
_presenter = new MainPresenter(_mockPortfolioRepository);
_presenter.Initialize(_mockView);
};
}
public class When_initializing_the_Main_Presenter : Given_an_initialized_Main_Presenter
{
It should_get_the_portfolio_list = () =>
Mock.Assert(() => _mockPortfolioRepository.GetAllPortfolios(), Occurs.Exactly(1));
It should_refresh_the_view = () =>
Mock.Assert(() => _mockView.RefreshList(Arg.IsAny<IList<string>>()), Occurs.Exactly(1));
It should_refresh_the_view_with_the_portfolio_list = () =>
Mock.Assert(() => _mockView.RefreshList(Arg.Matches<IList<string>>(
stringList => stringList.SequenceEqual(_portfolioList.Select(p => p.Name))
)));
}
public class When_the_user_wants_to_add_a_new_portfolio : Given_an_initialized_Main_Presenter
{
Because of = () =>
_presenter.AddNewPortfolio();
It should_tell_the_view_to_bring_up_the_Add_new_portfolio_screen = () =>
Mock.Assert(() => _mockView.StartAddPortfolioActivity(), Occurs.Exactly(1));
}
public class When_the_user_wants_to_view_a_portfolio : Given_an_initialized_Main_Presenter
{
Because of = () =>
_presenter.ViewPortfolio(1);
It should_tell_the_view_to_bring_up_the_View_Portfolio_screen_with_the_given_position = () =>
{
var id = _portfolioList[1].ID ?? -1;
Mock.Assert(() => _mockView.StartViewPortfolioActivity(id), Occurs.Exactly(1));
};
}
public class When_the_user_wants_to_delete_a_portfolio : Given_an_initialized_Main_Presenter
{
Because of = () =>
_presenter.DeletePortfolio(990099);
It should_use_the_repo_to_delete_the_portfolio_with_the_given_ID = () =>
Mock.Assert(() => _mockPortfolioRepository.DeletePortfolioById(990099), Occurs.Exactly(1));
}
public class When_the_user_wants_to_edit_a_portfolio : Given_an_initialized_Main_Presenter
{
Because of = () =>
_presenter.EditPortfolio(909);
It should_tell_the_view_to_start_up_an_edit_activity_for_the_given_portfolio_id = () =>
Mock.Assert(() => _mockView.StartEditPortfolioActivity(909), Occurs.Exactly(1));
}
public class When_the_user_wants_to_configure_the_display_fields : Given_an_initialized_Main_Presenter
{
Because of = () =>
_presenter.GotoConfig();
It should_tell_the_view_to_start_up_the_config_activity = () =>
Mock.Assert(() => _mockView.StartConfigActivity(), Occurs.Exactly(1));
}
public class When_the_user_wants_to_exit_the_app : Given_an_initialized_Main_Presenter
{
Because of = () =>
_presenter.ExitApplication();
It should_tell_the_view_to_start_up_the_config_activity = () =>
Mock.Assert(() => _mockView.ExitApplication(), Occurs.Exactly(1));
}
public class When_the_user_wants_to_see_the_context_menu : Given_an_initialized_Main_Presenter
{
static int _id;
Because of = () =>
_id = _presenter.GetPortfolioIdForContextMenu(_portfolio1.Name);
It should_use_the_given_name_to_lookup_the_ID = () =>
{
Mock.Assert(() => _mockPortfolioRepository.GetPortfolioByName(_portfolio1.Name), Occurs.Exactly(1));
_portfolio1.ID.ShouldEqual(_id);
};
_presenter = new MainPresenter(_mockPortfolioRepository);
_presenter.Initialize(_mockView);
};
}
}

View file

@ -8,7 +8,6 @@ using MonoStockPortfolio.Core.Services;
using MonoStockPortfolio.Entities;
using MonoStockPortfolio.Framework;
using Telerik.JustMock;
using Telerik.JustMock.Helpers;
namespace MonoStockPortfolio.Tests.Presenters
{
@ -48,60 +47,4 @@ namespace MonoStockPortfolio.Tests.Presenters
func.Invoke(null);
}
}
public class When_done_initializing_a_Portfolio_Presenter : Given_an_initialized_Portfolio_Presenter
{
It should_show_the_progress_bar_with_a_nice_message = () =>
Mock.Assert(() => _mockView.ShowProgressDialog("Loading...Please wait..."), Occurs.Exactly(1));
It should_use_the_service_to_get_the_positions = () =>
Mock.Assert(() => _mockPortfolioService.GetDetailedItems(999, Arg.IsAny<IEnumerable<StockDataItem>>()), Occurs.Exactly(1));
It should_hide_the_progress_bar_message = () =>
Mock.Assert(() => _mockView.HideProgressDialog(), Occurs.Exactly(1));
It should_refresh_the_view = () =>
Mock.Assert(() => _mockView.RefreshList(Arg.IsAny<IEnumerable<PositionResultsViewModel>>(), Arg.IsAny<IEnumerable<StockDataItem>>()), Occurs.Exactly(1));
It should_get_the_portfolio_name_from_the_repository_and_set_the_title_with_that = () =>
Mock.Assert(() => _mockView.SetTitle("Portfolio: Test Portfolio"), Occurs.Exactly(1));
}
public class When_the_user_wants_to_add_a_new_position : Given_an_initialized_Portfolio_Presenter
{
Because of = () =>
_presenter.AddNewPosition();
It should_tell_the_view_to_bring_up_the_Add_new_portfolio_screen = () =>
Mock.Assert(() => _mockView.StartAddNewPosition(999), Occurs.Exactly(1));
}
public class When_the_user_selects_edit_context_option : Given_an_initialized_Portfolio_Presenter
{
Because of = () =>
_presenter.ContextOptionSelected("Edit", 123);
It should_bring_up_the_edit_screen = () =>
Mock.Assert(() => _mockView.StartEditPosition(123, 999), Occurs.Exactly(1));
}
public class When_the_user_selects_delete_context_option : Given_an_initialized_Portfolio_Presenter
{
Because of = () =>
_presenter.ContextOptionSelected("Delete", 123);
It should_use_the_repo_to_delete_the_position = () =>
Mock.Assert(() => _mockPortfolioRepository.DeletePositionById(123), Occurs.Exactly(1));
}
public class When_the_user_wants_to_refresh_the_positions : Given_an_initialized_Portfolio_Presenter
{
Because of = () =>
_presenter.RefreshPositions();
It should_show_the_progress_bar_with_a_nice_message_again = () =>
Mock.Assert(() => _mockView.ShowProgressDialog("Loading...Please wait..."), Occurs.Exactly(2));
It should_use_the_service_to_get_the_positions_again = () =>
Mock.Assert(() => _mockPortfolioService.GetDetailedItems(999, Arg.IsAny<IEnumerable<StockDataItem>>()), Occurs.Exactly(2));
It should_hide_the_progress_bar_message_again = () =>
Mock.Assert(() => _mockView.HideProgressDialog(), Occurs.Exactly(2));
It should_refresh_the_view_again = () =>
Mock.Assert(() => _mockView.RefreshList(Arg.IsAny<IEnumerable<PositionResultsViewModel>>(), Arg.IsAny<IEnumerable<StockDataItem>>()), Occurs.Exactly(2));
}
}

View file

@ -0,0 +1,22 @@
using System.Collections.Generic;
using Machine.Specifications;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_done_initializing_a_Portfolio_Presenter : Given_an_initialized_Portfolio_Presenter
{
It should_show_the_progress_bar_with_a_nice_message = () =>
Mock.Assert(() => _mockView.ShowProgressDialog("Loading...Please wait..."), Occurs.Exactly(1));
It should_use_the_service_to_get_the_positions = () =>
Mock.Assert(() => _mockPortfolioService.GetDetailedItems(999, Arg.IsAny<IEnumerable<StockDataItem>>()), Occurs.Exactly(1));
It should_hide_the_progress_bar_message = () =>
Mock.Assert(() => _mockView.HideProgressDialog(), Occurs.Exactly(1));
It should_refresh_the_view = () =>
Mock.Assert(() => _mockView.RefreshList(Arg.IsAny<IEnumerable<PositionResultsViewModel>>(), Arg.IsAny<IEnumerable<StockDataItem>>()), Occurs.Exactly(1));
It should_get_the_portfolio_name_from_the_repository_and_set_the_title_with_that = () =>
Mock.Assert(() => _mockView.SetTitle("Portfolio: Test Portfolio"), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Linq;
using Machine.Specifications;
using MonoStockPortfolio.Core;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_initialize_the_config_presenter : Given_a_Config_Presenter
{
static List<StockDataItem> _allStockDataItems;
Establish context = () =>
{
_allStockDataItems = StockDataItem.Volume.GetValues<StockDataItem>().ToList<StockDataItem>();
};
Because of = () =>
{
_presenter.Initialize(_configView);
};
It should_send_two_checked_items = () =>
Mock.Assert(() => _configView.PrepopulateConfiguration(Arg.IsAny<IList<StockDataItem>>(), Arg.Matches<IEnumerable<StockDataItem>>(i => i.Count() == 2)), Occurs.Exactly(1));
It should_send_GainLoss_as_a_checked_item = () =>
Mock.Assert(() => _configView.PrepopulateConfiguration(Arg.IsAny<IList<StockDataItem>>(), Arg.Matches<IEnumerable<StockDataItem>>(i => i.Any(p => p == StockDataItem.GainLoss))), Occurs.Exactly(1));
It should_send_Change_as_a_checked_item = () =>
Mock.Assert(() => _configView.PrepopulateConfiguration(Arg.IsAny<IList<StockDataItem>>(), Arg.Matches<IEnumerable<StockDataItem>>(i => i.Any(p => p == StockDataItem.Change))), Occurs.Exactly(1));
It should_send_an_enumerated_list_of_all_stock_items = () =>
Mock.Assert(() => _configView.PrepopulateConfiguration(Arg.Matches<IList<StockDataItem>>(i => i.Count == _allStockDataItems.Count), Arg.IsAny<IEnumerable<StockDataItem>>()), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Linq;
using Machine.Specifications;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_initializing_the_Main_Presenter : Given_an_initialized_Main_Presenter
{
It should_get_the_portfolio_list = () =>
Mock.Assert(() => _mockPortfolioRepository.GetAllPortfolios(), Occurs.Exactly(1));
It should_refresh_the_view = () =>
Mock.Assert(() => _mockView.RefreshList(Arg.IsAny<IList<string>>()), Occurs.Exactly(1));
It should_refresh_the_view_with_the_portfolio_list = () =>
Mock.Assert(() => _mockView.RefreshList(Arg.Matches<IList<string>>(stringList => stringList.SequenceEqual(_portfolioList.Select(p => p.Name)))));
}
}

View file

@ -0,0 +1,20 @@
using Machine.Specifications;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_initializing_the_edit_portfolio_presenter_with_an_id : EditPortfolioTests
{
Because of = () =>
{
_presenter.Initialize(_mockEditPortfolioView, 999);
};
It should_set_the_title_to_Edit_Portfolio = () =>
Mock.Assert(() => _mockEditPortfolioView.SetTitle("Edit Portfolio"), Occurs.Exactly(1));
It should_prepopulate_the_form_with_a_portfolio_name = () =>
Mock.Assert(() => _mockEditPortfolioView.PopulateForm(Arg.Matches<Portfolio>(x => x.Name == "Testing Portfolio!")), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,20 @@
using Machine.Specifications;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_initializing_the_edit_portfolio_presenter_with_no_id : EditPortfolioTests
{
Because of = () =>
{
_presenter.Initialize(_mockEditPortfolioView, null);
};
It should_set_the_title_to_Add_New_Portfolio = () =>
Mock.Assert(() => _mockEditPortfolioView.SetTitle("Add New Portfolio"), Occurs.Exactly(1));
It shouldnt_prepopulate_the_form_with_anything = () =>
Mock.Assert(() => _mockEditPortfolioView.PopulateForm(Arg.IsAny<Portfolio>()), Occurs.Never());
}
}

View file

@ -0,0 +1,30 @@
using Machine.Specifications;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_initializing_the_edit_position_presenter_with_an_id : EditPositionTests
{
Establish context = () =>
{
var fakePosition = new Position(999) { ContainingPortfolioID = 1, PricePerShare = 5.99M, Shares = 50M, Ticker = "FAKE" };
Mock.Arrange(() => _mockPortfolioRepository.GetPositionById(999)).Returns(fakePosition);
};
Because of = () =>
{
_presenter.Initialize(_mockView, 1, 999);
};
It should_set_the_title_to_Edit_Position = () =>
Mock.Assert(() => _mockView.SetTitle("Edit Position"), Occurs.Exactly(1));
It should_prepopulate_the_PricePerShare_on_the_form = () =>
Mock.Assert(() => _mockView.PopulateForm(Arg.Matches<Position>(p => p.PricePerShare == 5.99M)), Occurs.Exactly(1));
It should_prepopulate_the_Shares_on_the_form = () =>
Mock.Assert(() => _mockView.PopulateForm(Arg.Matches<Position>(p => p.Shares == 50M)), Occurs.Exactly(1));
It should_prepopulate_the_Ticker_on_the_form = () =>
Mock.Assert(() => _mockView.PopulateForm(Arg.Matches<Position>(p => p.Ticker == "FAKE")), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,20 @@
using Machine.Specifications;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_initializing_the_edit_position_presenter_with_no_id : EditPositionTests
{
Because of = () =>
{
_presenter.Initialize(_mockView, 1);
};
It should_set_the_title_to_Add_Position = () =>
Mock.Assert(() => _mockView.SetTitle("Add Position"), Occurs.Exactly(1));
It shouldnt_prepopulate_the_form_with_anything = () =>
Mock.Assert(() => _mockView.PopulateForm(Arg.IsAny<Position>()),Occurs.Never());
}
}

View file

@ -0,0 +1,15 @@
using Machine.Specifications;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_selects_delete_context_option : Given_an_initialized_Portfolio_Presenter
{
Because of = () =>
_presenter.ContextOptionSelected("Delete", 123);
It should_use_the_repo_to_delete_the_position = () =>
Mock.Assert(() => _mockPortfolioRepository.DeletePositionById(123), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,15 @@
using Machine.Specifications;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_selects_edit_context_option : Given_an_initialized_Portfolio_Presenter
{
Because of = () =>
_presenter.ContextOptionSelected("Edit", 123);
It should_bring_up_the_edit_screen = () =>
Mock.Assert(() => _mockView.StartEditPosition(123, 999), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Linq;
using Machine.Specifications;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_tries_to_save_a_new_portfolio_with_a_blank_name : EditPortfolioTests
{
Establish context = () =>
{
_presenter.Initialize(_mockEditPortfolioView);
};
Because of = () =>
{
_presenter.SavePortfolio(new Portfolio {Name = ""});
};
It should_return_1_validation_error = () =>
Mock.Assert(() => _mockEditPortfolioView.ShowValidationErrors(Arg.Matches<IEnumerable<string>>(x => x.Count() == 1)), Occurs.Exactly(1));
It should_return_a_nice_required_validation_error_message = () =>
Mock.Assert(() => _mockEditPortfolioView.ShowValidationErrors(Arg.Matches<IEnumerable<string>>(x => x.Single() == "Please enter a portfolio name")), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Linq;
using Machine.Specifications;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_tries_to_save_a_portfolio_with_a_duplicated_name : EditPortfolioTests
{
Establish context = () =>
{
_presenter.Initialize(_mockEditPortfolioView);
};
Because of = () =>
{
Mock.Arrange(() => _mockPortfolioRepository.GetPortfolioByName(Arg.AnyString)).Returns(
new Portfolio(998) {Name = "Some Name"});
_presenter.SavePortfolio(new Portfolio {Name = "Some Name"});
};
It should_return_1_validation_error = () =>
Mock.Assert(() => _mockEditPortfolioView.ShowValidationErrors(Arg.Matches<IEnumerable<string>>(x => x.Count() == 1)), Occurs.Exactly(1));
It should_return_a_nice_duplication_error_message = () =>
Mock.Assert(() => _mockEditPortfolioView.ShowValidationErrors(Arg.Matches<IEnumerable<string>>(x => x.Single() == "Portfolio name is already taken")), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,15 @@
using Machine.Specifications;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_add_a_new_portfolio : Given_an_initialized_Main_Presenter
{
Because of = () =>
_presenter.AddNewPortfolio();
It should_tell_the_view_to_bring_up_the_Add_new_portfolio_screen = () =>
Mock.Assert(() => _mockView.StartAddPortfolioActivity(), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,15 @@
using Machine.Specifications;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_add_a_new_position : Given_an_initialized_Portfolio_Presenter
{
Because of = () =>
_presenter.AddNewPosition();
It should_tell_the_view_to_bring_up_the_Add_new_portfolio_screen = () =>
Mock.Assert(() => _mockView.StartAddNewPosition(999), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,15 @@
using Machine.Specifications;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_configure_the_display_fields : Given_an_initialized_Main_Presenter
{
Because of = () =>
_presenter.GotoConfig();
It should_tell_the_view_to_start_up_the_config_activity = () =>
Mock.Assert(() => _mockView.StartConfigActivity(), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,15 @@
using Machine.Specifications;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_delete_a_portfolio : Given_an_initialized_Main_Presenter
{
Because of = () =>
_presenter.DeletePortfolio(990099);
It should_use_the_repo_to_delete_the_portfolio_with_the_given_ID = () =>
Mock.Assert(() => _mockPortfolioRepository.DeletePortfolioById(990099), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,15 @@
using Machine.Specifications;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_edit_a_portfolio : Given_an_initialized_Main_Presenter
{
Because of = () =>
_presenter.EditPortfolio(909);
It should_tell_the_view_to_start_up_an_edit_activity_for_the_given_portfolio_id = () =>
Mock.Assert(() => _mockView.StartEditPortfolioActivity(909), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,15 @@
using Machine.Specifications;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_exit_the_app : Given_an_initialized_Main_Presenter
{
Because of = () =>
_presenter.ExitApplication();
It should_tell_the_view_to_start_up_the_config_activity = () =>
Mock.Assert(() => _mockView.ExitApplication(), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,23 @@
using System.Collections.Generic;
using Machine.Specifications;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_refresh_the_positions : Given_an_initialized_Portfolio_Presenter
{
Because of = () =>
_presenter.RefreshPositions();
It should_show_the_progress_bar_with_a_nice_message_again = () =>
Mock.Assert(() => _mockView.ShowProgressDialog("Loading...Please wait..."), Occurs.Exactly(2));
It should_use_the_service_to_get_the_positions_again = () =>
Mock.Assert(() => _mockPortfolioService.GetDetailedItems(999, Arg.IsAny<IEnumerable<StockDataItem>>()), Occurs.Exactly(2));
It should_hide_the_progress_bar_message_again = () =>
Mock.Assert(() => _mockView.HideProgressDialog(), Occurs.Exactly(2));
It should_refresh_the_view_again = () =>
Mock.Assert(() => _mockView.RefreshList(Arg.IsAny<IEnumerable<PositionResultsViewModel>>(), Arg.IsAny<IEnumerable<StockDataItem>>()), Occurs.Exactly(2));
}
}

View file

@ -0,0 +1,27 @@
using Machine.Specifications;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_save_a_valid_portfolio : EditPortfolioTests
{
Establish context = () =>
{
_presenter.Initialize(_mockEditPortfolioView, null);
};
Because of = () =>
{
_presenter.SavePortfolio(new Portfolio(999) {Name = "Whatever Portfolio"});
};
It should_use_the_repository_to_save_the_portfolio = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePortfolio(Arg.Matches<Portfolio>(x => x.ID == 999 && x.Name == "Whatever Portfolio")), Occurs.Exactly(1));
It should_tell_the_view_to_show_a_nice_saved_message = () =>
Mock.Assert(() => _mockEditPortfolioView.ShowSaveSuccessMessage("You saved: Whatever Portfolio"), Occurs.Exactly(1));
It should_tell_the_view_to_go_back_to_the_main_activity = () =>
Mock.Assert(() => _mockEditPortfolioView.GoBackToMainActivity(), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,37 @@
using Machine.Specifications;
using MonoStockPortfolio.Activites.EditPositionScreen;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_save_a_valid_position : EditPositionTests
{
Establish context = () =>
{
_presenter.Initialize(_mockView, 1);
Mock.Arrange(() => _mockStockService.IsValidTicker(Arg.AnyString)).Returns(true);
};
Because of = () =>
{
var fakeInputModel = new PositionInputModel {PriceText = "2.34", SharesText = "671", TickerText = "LOL"};
_presenter.Save(fakeInputModel);
};
It should_save_a_position_with_the_portfolio_repository = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.IsAny<Position>()), Occurs.Exactly(1));
It should_save_a_position_with_the_correct_Price = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.Matches<Position>(p => p.PricePerShare == 2.34M)), Occurs.Exactly(1));
It should_save_a_position_with_the_correct_Shares = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.Matches<Position>(p => p.Shares == 671M)), Occurs.Exactly(1));
It should_save_a_position_with_the_correct_Ticker = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.Matches<Position>(p => p.Ticker == "LOL")), Occurs.Exactly(1));
It should_save_a_position_with_the_correct_Containing_Portfolio_ID = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.Matches<Position>(p => p.ContainingPortfolioID == 1)), Occurs.Exactly(1));
It should_tell_the_view_to_go_back_to_the_main_activity = () =>
Mock.Assert(() => _mockView.GoBackToMainActivity(), Occurs.Exactly(1));
}
}

View file

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Machine.Specifications;
using MonoStockPortfolio.Activites.EditPositionScreen;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_save_an_invalid_position : EditPositionTests
{
Establish context = () =>
{
_presenter.Initialize(_mockView, 1);
Mock.Arrange(() => _mockStockService.IsValidTicker(Arg.AnyString)).Returns(false);
};
Because of = () =>
{
var fakeInputModel = new PositionInputModel {PriceText = "cows", SharesText = "WALRUS!!", TickerText = "fail"};
_presenter.Save(fakeInputModel);
};
It should_not_try_to_save_the_portfolio = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.IsAny<Position>()), Occurs.Never());
It should_send_the_validation_errors_to_the_view = () =>
Mock.Assert(() => _mockView.ShowErrorMessages(Arg.IsAny<IList<string>>()), Occurs.Exactly(1));
It should_send_an_invalid_ticker_error_to_the_view = () =>
MockAssertPositionMatches(x => x.Any(p => p == "Invalid Ticker Name"));
It should_send_an_invalid_shares_number_error_to_the_view = () =>
MockAssertPositionMatches(x => x.Any(p => p == "Please enter a valid, positive number of shares"));
It should_send_an_invalid_price_per_share_error_to_the_view = () =>
MockAssertPositionMatches(x => x.Any(p => p == "Please enter a valid, positive price per share"));
It should_not_tell_the_view_to_go_back_to_the_main_activity = () =>
Mock.Assert(() => _mockView.GoBackToMainActivity(), Occurs.Never());
private static void MockAssertPositionMatches(Expression<Predicate<IList<string>>> match)
{
Mock.Assert(() => _mockView.ShowErrorMessages(Arg.Matches(match)), Occurs.Exactly(1));
}
}
}

View file

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Machine.Specifications;
using MonoStockPortfolio.Activites.EditPositionScreen;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_save_an_invalid_position_with_blank_fields : EditPositionTests
{
Establish context = () =>
{
_presenter.Initialize(_mockView, 1);
Mock.Arrange(() => _mockStockService.IsValidTicker(Arg.AnyString)).Returns(false);
};
Because of = () =>
{
var fakeInputModel = new PositionInputModel { PriceText = "", SharesText = "", TickerText = "" };
_presenter.Save(fakeInputModel);
};
It should_not_try_to_save_the_portfolio = () =>
Mock.Assert(() => _mockPortfolioRepository.SavePosition(Arg.IsAny<Position>()), Occurs.Never());
It should_send_the_validation_errors_to_the_view = () =>
Mock.Assert(() => _mockView.ShowErrorMessages(Arg.IsAny<IList<string>>()), Occurs.Exactly(1));
It should_send_an_invalid_ticker_error_to_the_view = () =>
MockPositionMatches(x => x.Any(p => p == "Please enter a ticker"));
It should_send_an_invalid_shares_number_error_to_the_view = () =>
MockPositionMatches(x => x.Any(p => p == "Please enter a valid, positive number of shares"));
It should_send_an_invalid_price_per_share_error_to_the_view = () =>
MockPositionMatches(x => x.Any(p => p == "Please enter a valid, positive price per share"));
It should_not_tell_the_view_to_go_back_to_the_main_activity = () =>
Mock.Assert(() => _mockView.GoBackToMainActivity(), Occurs.Never());
private static void MockPositionMatches(Expression<Predicate<IList<string>>> match)
{
Mock.Assert(() => _mockView.ShowErrorMessages(Arg.Matches(match)), Occurs.Exactly(1));
}
}
}

View file

@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Linq;
using Machine.Specifications;
using MonoStockPortfolio.Entities;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_save_configuration : Given_a_Config_Presenter
{
Establish context = () =>
{
_presenter.Initialize(_configView);
};
Because of = () =>
{
_presenter.SaveConfig(new List<StockDataItem> {StockDataItem.Ticker, StockDataItem.Time});
};
It should_use_the_repo_to_update_to_2_stock_items = () =>
Mock.Assert(() => _configRepository.UpdateStockItems(Arg.Matches<List<StockDataItem>>(i => i.Count == 2)));
It should_use_the_repo_to_update_stock_items_with_Time = () =>
Mock.Assert(() => _configRepository.UpdateStockItems(Arg.Matches<List<StockDataItem>>(i => i.Any(s => s == StockDataItem.Time))));
It should_use_the_repo_to_update_stock_items_with_Ticker = () =>
Mock.Assert(() => _configRepository.UpdateStockItems(Arg.Matches<List<StockDataItem>>(i => i.Any(s => s == StockDataItem.Ticker))));
}
}

View file

@ -0,0 +1,21 @@
using Machine.Specifications;
using MonoStockPortfolio.Tests.Presenters;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Activities
{
[Tags("UnitTest")]
public class When_the_user_wants_to_see_the_context_menu : Given_an_initialized_Main_Presenter
{
static int _id;
Because of = () =>
_id = _presenter.GetPortfolioIdForContextMenu(_portfolio1.Name);
It should_use_the_given_name_to_lookup_the_ID = () =>
{
Mock.Assert(() => _mockPortfolioRepository.GetPortfolioByName(_portfolio1.Name), Occurs.Exactly(1));
_portfolio1.ID.ShouldEqual(_id);
};
}
}

View file

@ -0,0 +1,19 @@
using Machine.Specifications;
using Telerik.JustMock;
namespace MonoStockPortfolio.Tests.Presenters
{
[Tags("UnitTest")]
public class When_the_user_wants_to_view_a_portfolio : Given_an_initialized_Main_Presenter
{
Because of = () =>
_presenter.ViewPortfolio(1);
It should_tell_the_view_to_bring_up_the_View_Portfolio_screen_with_the_given_position = () =>
{
var id = _portfolioList[1].ID ?? -1;
Mock.Assert(() => _mockView.StartViewPortfolioActivity(id), Occurs.Exactly(1));
};
}
}

View file

@ -0,0 +1,29 @@
using Machine.Specifications;
using MonoStockPortfolio.Core.StockData;
namespace MonoStockPortfolio.Tests.Services
{
[Tags("IntegrationTest")]
public class When_using_the_Yahoo_stock_data_service_to_validate_tickers
{
static YahooStockDataProvider _svc;
static bool _goodTicker;
static bool _badTicker;
Establish context = () =>
{
_svc = new YahooStockDataProvider();
};
Because of = () =>
{
_goodTicker = _svc.IsValidTicker("GOOG");
_badTicker = _svc.IsValidTicker("GOOGAMOOGA");
};
It should_validate_the_good_ticker = () =>
_goodTicker.ShouldBeTrue();
It shouldnt_validate_the_bad_ticker = () =>
_badTicker.ShouldBeFalse();
}
}

View file

@ -7,6 +7,7 @@ using MonoStockPortfolio.Entities;
namespace MonoStockPortfolio.Tests.Services
{
[Tags("IntegrationTest")]
public class When_using_the_Yahoo_stock_data_service_to_get_quotes
{
static YahooStockDataProvider _svc;
@ -30,27 +31,4 @@ namespace MonoStockPortfolio.Tests.Services
It should_get_price_change_from_the_web = () =>
_quotes.ForEach(q => q.Change.ShouldNotEqual(0.0M));
}
public class When_using_the_Yahoo_stock_data_service_to_validate_tickers
{
static YahooStockDataProvider _svc;
static bool _goodTicker;
static bool _badTicker;
Establish context = () =>
{
_svc = new YahooStockDataProvider();
};
Because of = () =>
{
_goodTicker = _svc.IsValidTicker("GOOG");
_badTicker = _svc.IsValidTicker("GOOGAMOOGA");
};
It should_validate_the_good_ticker = () =>
_goodTicker.ShouldBeTrue();
It shouldnt_validate_the_bad_ticker = () =>
_badTicker.ShouldBeFalse();
}
}

View file

@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.5420
// Runtime Version:2.0.50727.4952
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.