MonodroidStockPortfolio/MonoStockPortfolio.Tests/Presenters/MainPresenterTests.cs
2011-03-20 16:18:14 -04:00

124 lines
No EOL
5.2 KiB
C#

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
{
public class Given_an_initialized_Main_Presenter
{
protected static IMainPresenter _presenter;
protected static IPortfolioRepository _mockPortfolioRepository;
protected static IMainView _mockView;
protected static IList<Portfolio> _portfolioList;
protected static Portfolio _portfolio1;
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);
_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);
};
}
}