From 8d9e6d4919c3fff5bfb9223ff2a52092b5e23ccf Mon Sep 17 00:00:00 2001 From: "Matthew D. Groves" Date: Sun, 20 Mar 2011 16:18:14 -0400 Subject: [PATCH] refactored tests out of xunit --- .../Framework/ValidationTests.cs | 28 ----- ...validating_forms_with_validation_errors.cs | 38 ++++++ .../MonoStockPortfolio.Tests.csproj | 9 +- .../Presenters/EditPortfolioTests.cs | 1 + .../Presenters/EditPositionTests.cs | 1 + .../Presenters/MainPresenterTests.cs | 2 +- .../Presenters/PortfolioPresenterTests.cs | 1 + .../Services/YahooStockDataServiceTests.cs | 56 +++++++++ .../YahooStockDataProviderTests.cs | 109 ------------------ MonoStockPortfolio.sln | 1 + .../Resources/Resource.Designer.cs | 2 +- 11 files changed, 103 insertions(+), 145 deletions(-) delete mode 100644 MonoStockPortfolio.Tests/Framework/ValidationTests.cs create mode 100644 MonoStockPortfolio.Tests/Framework/When_validating_forms_with_validation_errors.cs create mode 100644 MonoStockPortfolio.Tests/Services/YahooStockDataServiceTests.cs delete mode 100644 MonoStockPortfolio.Tests/YahooStockDataProviderTests.cs diff --git a/MonoStockPortfolio.Tests/Framework/ValidationTests.cs b/MonoStockPortfolio.Tests/Framework/ValidationTests.cs deleted file mode 100644 index 7781c94..0000000 --- a/MonoStockPortfolio.Tests/Framework/ValidationTests.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Machine.Specifications; -using MonoStockPortfolio.Activites.EditPortfolioScreen; -using MonoStockPortfolio.Framework; - -namespace MonoStockPortfolio.Tests.Framework -{ - public class ValidationTests - { - static FormValidator _validator; - static IEnumerable _errors; - - Establish context = () => - { - _validator = new FormValidator(); - }; - - Because of = () => - { - _validator.AddRequired(() => "", "This is required"); - _errors = _validator.Apply(); - }; - - It should_return_1_error_message = () => - _errors.Count().ShouldEqual(1); - } -} \ No newline at end of file diff --git a/MonoStockPortfolio.Tests/Framework/When_validating_forms_with_validation_errors.cs b/MonoStockPortfolio.Tests/Framework/When_validating_forms_with_validation_errors.cs new file mode 100644 index 0000000..b7ee5ac --- /dev/null +++ b/MonoStockPortfolio.Tests/Framework/When_validating_forms_with_validation_errors.cs @@ -0,0 +1,38 @@ +using System.Collections.Generic; +using System.Linq; +using Machine.Specifications; +using MonoStockPortfolio.Framework; + +namespace MonoStockPortfolio.Tests.Framework +{ + public class When_validating_forms_with_validation_errors + { + static FormValidator _validator; + static IEnumerable _errors; + + Establish context = () => + { + _validator = new FormValidator(); + }; + + Because of = () => + { + _validator.AddRequired(() => "", "This is required"); + _validator.AddValidDecimal(() => "not a decimal", "Decimal required"); + _validator.AddValidPositiveDecimal(() => "-18.9", "Positive decimal required"); + _validator.AddValidation(() => "arbitrary error!"); + _errors = _validator.Apply(); + }; + + It should_return_1_error_message = () => + _errors.Count().ShouldEqual(4); + It should_have_a_required_message = () => + _errors.Any(e => e == "This is required").ShouldBeTrue(); + It should_have_a_valid_decimal_message = () => + _errors.Any(e => e == "Decimal required").ShouldBeTrue(); + It should_have_a_valid_positive_decimal_message = () => + _errors.Any(e => e == "Positive decimal required").ShouldBeTrue(); + It should_have_an_arbitrary_message = () => + _errors.Any(e => e == "arbitrary error!").ShouldBeTrue(); + } +} \ No newline at end of file diff --git a/MonoStockPortfolio.Tests/MonoStockPortfolio.Tests.csproj b/MonoStockPortfolio.Tests/MonoStockPortfolio.Tests.csproj index 17bf125..0a39d47 100644 --- a/MonoStockPortfolio.Tests/MonoStockPortfolio.Tests.csproj +++ b/MonoStockPortfolio.Tests/MonoStockPortfolio.Tests.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + 169 pdbonly @@ -56,19 +57,15 @@ ..\libs\Telerik.JustMock.Silverlight.dll - - False - ..\libs\xunit.dll - - + - + diff --git a/MonoStockPortfolio.Tests/Presenters/EditPortfolioTests.cs b/MonoStockPortfolio.Tests/Presenters/EditPortfolioTests.cs index b5b468e..8e0611c 100644 --- a/MonoStockPortfolio.Tests/Presenters/EditPortfolioTests.cs +++ b/MonoStockPortfolio.Tests/Presenters/EditPortfolioTests.cs @@ -5,6 +5,7 @@ using MonoStockPortfolio.Activites.EditPortfolioScreen; using MonoStockPortfolio.Core.PortfolioRepositories; using MonoStockPortfolio.Entities; using Telerik.JustMock; +using Telerik.JustMock.Helpers; namespace MonoStockPortfolio.Tests.Presenters { diff --git a/MonoStockPortfolio.Tests/Presenters/EditPositionTests.cs b/MonoStockPortfolio.Tests/Presenters/EditPositionTests.cs index 38ed9e9..d496a71 100644 --- a/MonoStockPortfolio.Tests/Presenters/EditPositionTests.cs +++ b/MonoStockPortfolio.Tests/Presenters/EditPositionTests.cs @@ -8,6 +8,7 @@ using MonoStockPortfolio.Core.PortfolioRepositories; using MonoStockPortfolio.Core.StockData; using MonoStockPortfolio.Entities; using Telerik.JustMock; +using Telerik.JustMock.Helpers; namespace MonoStockPortfolio.Tests.Presenters { diff --git a/MonoStockPortfolio.Tests/Presenters/MainPresenterTests.cs b/MonoStockPortfolio.Tests/Presenters/MainPresenterTests.cs index 66a7629..8887acd 100644 --- a/MonoStockPortfolio.Tests/Presenters/MainPresenterTests.cs +++ b/MonoStockPortfolio.Tests/Presenters/MainPresenterTests.cs @@ -5,7 +5,7 @@ using MonoStockPortfolio.Activites.MainScreen; using MonoStockPortfolio.Core.PortfolioRepositories; using MonoStockPortfolio.Entities; using Telerik.JustMock; -using Xunit; +using Telerik.JustMock.Helpers; namespace MonoStockPortfolio.Tests.Activities { diff --git a/MonoStockPortfolio.Tests/Presenters/PortfolioPresenterTests.cs b/MonoStockPortfolio.Tests/Presenters/PortfolioPresenterTests.cs index 47268b7..b92e4ee 100644 --- a/MonoStockPortfolio.Tests/Presenters/PortfolioPresenterTests.cs +++ b/MonoStockPortfolio.Tests/Presenters/PortfolioPresenterTests.cs @@ -8,6 +8,7 @@ using MonoStockPortfolio.Core.Services; using MonoStockPortfolio.Entities; using MonoStockPortfolio.Framework; using Telerik.JustMock; +using Telerik.JustMock.Helpers; namespace MonoStockPortfolio.Tests.Presenters { diff --git a/MonoStockPortfolio.Tests/Services/YahooStockDataServiceTests.cs b/MonoStockPortfolio.Tests/Services/YahooStockDataServiceTests.cs new file mode 100644 index 0000000..331cb5c --- /dev/null +++ b/MonoStockPortfolio.Tests/Services/YahooStockDataServiceTests.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; +using System.Linq; +using Machine.Specifications; +using Machine.Specifications.Runner.Impl; +using MonoStockPortfolio.Core.StockData; +using MonoStockPortfolio.Entities; + +namespace MonoStockPortfolio.Tests.Services +{ + public class When_using_the_Yahoo_stock_data_service_to_get_quotes + { + static YahooStockDataProvider _svc; + static IList _quotes; + + Establish context = () => + { + _svc = new YahooStockDataProvider(); + }; + + Because of = () => + { + _quotes = _svc.GetStockQuotes(new[] { "GOOG", "AMZN", "AAPL", "MSFT", "NOVL", "S", "VZ", "T" }) + .ToList(); + }; + + It should_get_volumes_from_the_web = () => + _quotes.ForEach(q => string.IsNullOrEmpty(q.Volume).ShouldBeFalse()); + It should_get_last_trade_prices_from_the_web = () => + _quotes.ForEach(q => q.LastTradePrice.ShouldNotEqual(0.0M)); + 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(); + } +} \ No newline at end of file diff --git a/MonoStockPortfolio.Tests/YahooStockDataProviderTests.cs b/MonoStockPortfolio.Tests/YahooStockDataProviderTests.cs deleted file mode 100644 index 69eb406..0000000 --- a/MonoStockPortfolio.Tests/YahooStockDataProviderTests.cs +++ /dev/null @@ -1,109 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using MonoStockPortfolio.Core.PortfolioRepositories; -using MonoStockPortfolio.Core.Services; -using MonoStockPortfolio.Core.StockData; -using MonoStockPortfolio.Entities; -using Xunit; - -namespace MonoStockPortfolio.Tests -{ - public class YahooStockDataProviderTests - { - [Fact] - public void Can_get_volume_from_web() - { - var svc = new YahooStockDataProvider(); - var quotes = svc.GetStockQuotes(new[] {"GOOG", "AMZN", "AAPL", "MSFT", "NOVL", "S", "VZ", "T"}); - foreach (var stockQuote in quotes) - { - Assert.True(!string.IsNullOrEmpty(stockQuote.Volume)); - } - Assert.True(quotes.Any()); - } - - [Fact] - public void Can_get_volume_from_service() - { - var svc = new PortfolioService(BuildStubPortfolioRepo(), new YahooStockDataProvider()); - var items = svc.GetDetailedItems(1, new List {StockDataItem.Ticker, StockDataItem.Volume}); - foreach (var positionResultsViewModel in items) - { - Assert.True(positionResultsViewModel.Items[StockDataItem.Volume] != null); - } - Assert.True(items.Any()); - } - - [Fact] - public void Can_tell_if_a_ticker_is_valid() - { - var svc = new YahooStockDataProvider(); - Assert.True(svc.IsValidTicker("GOOG")); - } - - [Fact] - public void Can_tell_if_a_ticker_is_invalid() - { - var svc = new YahooStockDataProvider(); - Assert.False(svc.IsValidTicker("HARBLHARBLHARBL")); - } - - private IPortfolioRepository BuildStubPortfolioRepo() - { - return new StubPortfolioRepo(); - } - - public class StubPortfolioRepo : IPortfolioRepository - { - public IList GetAllPositions(long portfolioId) - { - return new List - { - new Position(1) - {ContainingPortfolioID = 1, PricePerShare = 5, Shares = 100, Ticker = "GOOG"} - }; - } - - public IList GetAllPortfolios() - { - throw new NotImplementedException(); - } - - public void SavePortfolio(Portfolio portfolio) - { - throw new NotImplementedException(); - } - - public void DeletePortfolioById(int portfolioId) - { - throw new NotImplementedException(); - } - - public Portfolio GetPortfolioById(long portfolioId) - { - throw new NotImplementedException(); - } - - public Portfolio GetPortfolioByName(string portfolioName) - { - throw new NotImplementedException(); - } - - public void SavePosition(Position position) - { - throw new NotImplementedException(); - } - - public void DeletePositionById(long positionId) - { - throw new NotImplementedException(); - } - - public Position GetPositionById(long positionId) - { - throw new NotImplementedException(); - } - } - } -} \ No newline at end of file diff --git a/MonoStockPortfolio.sln b/MonoStockPortfolio.sln index e312ac3..2804436 100644 --- a/MonoStockPortfolio.sln +++ b/MonoStockPortfolio.sln @@ -6,6 +6,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "libs", "libs", "{643BA3D4-E3D6-49B7-B347-2B935FD5B9FC}" ProjectSection(SolutionItems) = preProject libs\PostSharp.SL.dll = libs\PostSharp.SL.dll + libs\Telerik.JustMock.Silverlight.dll = libs\Telerik.JustMock.Silverlight.dll EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoStockPortfolio.Core", "MonoStockPortfolio.Core\MonoStockPortfolio.Core.csproj", "{251E7BB4-CFE2-4DE4-9E2A-AAE1AF41C8CB}" diff --git a/MonoStockPortfolio/Resources/Resource.Designer.cs b/MonoStockPortfolio/Resources/Resource.Designer.cs index 35980d9..86b6bc8 100644 --- a/MonoStockPortfolio/Resources/Resource.Designer.cs +++ b/MonoStockPortfolio/Resources/Resource.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:2.0.50727.4952 +// Runtime Version:2.0.50727.5420 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated.