diff --git a/Refactoring.sln b/Refactoring.sln index 94e9f0e..0a92aec 100644 --- a/Refactoring.sln +++ b/Refactoring.sln @@ -1,8 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.21005.1 -MinimumVisualStudioVersion = 10.0.40219.1 +# Visual Studio 2012 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tusc", "Refactoring\Tusc.csproj", "{2D3F5D0E-4A6B-44C0-8F63-8E95243AC028}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestProject", "UnitTestProject\UnitTestProject.csproj", "{1FAF99A1-CCBF-435D-B599-4DAE1DAD5105}" @@ -25,4 +23,7 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(Performance) = preSolution + HasPerformanceSessions = true + EndGlobalSection EndGlobal diff --git a/Refactoring/Store.cs b/Refactoring/Store.cs index d9a7c78..48fcad6 100644 --- a/Refactoring/Store.cs +++ b/Refactoring/Store.cs @@ -32,7 +32,7 @@ public void Purchase(string productId, int quantity) throw new OutOfStockException(); } - product.Quantity = product.Quantity - quantity+1; + product.Quantity = product.Quantity - quantity; user.Balance = user.Balance - product.Price * quantity; dataManager.SaveUser(user); diff --git a/UnitTestProject/AuthenticatorTests.cs b/UnitTestProject/AuthenticatorTests.cs new file mode 100644 index 0000000..f32ce6f --- /dev/null +++ b/UnitTestProject/AuthenticatorTests.cs @@ -0,0 +1,52 @@ +using Newtonsoft.Json; +using NUnit.Framework; +using Refactoring; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UnitTestProject +{ + [TestFixture] + class AuthenticatorTests + { + private User testUser; + private Authenticator authenticator; + + [Test] + public void Test_Authenticate_ShouldSucceed() + { + User user = authenticator.Authenticate("Chris", ""); + Assert.AreEqual(testUser, user); + } + + [Test] + public void Test_Authenticate_InvalidPassword() + { + User user = authenticator.Authenticate("Chris", "asdf"); + Assert.Null(user); + } + + [Test] + public void Test_Authenticate_NullUserName() + { + User user = authenticator.Authenticate(null, ""); + Assert.Null(user); + } + + [SetUp] + public void Test_Initialize() + { + List users = new List(); + testUser = new User(); + testUser.Name = "Chris"; + testUser.Password = ""; + testUser.Balance = 100; + users.Add(testUser); + + authenticator = new Authenticator(users); + } + } +} diff --git a/UnitTestProject/IntegrationTests.cs b/UnitTestProject/IntegrationTests.cs index f526b8a..ddb4fb9 100644 --- a/UnitTestProject/IntegrationTests.cs +++ b/UnitTestProject/IntegrationTests.cs @@ -10,7 +10,7 @@ namespace UnitTestProject { [TestFixture] - //[Ignore("Disable integration tests")] + [Ignore("Disable integration tests")] public class IntegrationTests { private List users; diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 9f0b866..cd302ae 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -13,6 +13,38 @@ namespace UnitTestProject [TestFixture] class StoreTests { + private const String TEST_PRODUCT_ID = "1"; + + private Store createStore(double balance, double product_cost, int quantity) + { + var users = getUserInList("Test User", "", balance); + var products = getProductInList(TEST_PRODUCT_ID, "Product", product_cost, quantity); + + var dataManager = new DataManager(users, products); + return new Store(users[0], dataManager); + } + + private Store createStoreWithProducts(double balance, List products) + { + var users = getUserInList("Test User", "", balance); + var dataManager = new DataManager(users, products); + return new Store(users[0], dataManager); + } + + private List getUserInList(string name, string password, double balance) + { + var users = new List(); + users.Add(createTestUser("Test User", "", balance)); + return users; + } + + private List getProductInList(string id, string name, double price, int quantity) + { + var products = new List(); + products.Add(createTestProduct(id, "Product", price, quantity)); + return products; + } + private User createTestUser(string name, string password, double balance) { User testUser = new User(); @@ -37,60 +69,42 @@ private Product createTestProduct(string id, string name, double price, int quan [Test] public void Test_PurchaseThrowsNoErrorForValidFunds() { - //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); - users.Add(createTestUser("Test User", "", 99.99)); - - var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10)); + var store = createStore(99.9, 9, 10); - var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); - - //Act store.Purchase(TEST_PRODUCT_ID, 10); - //Assert Assert.Pass("No assertion really necessary here"); } [Test] public void Test_PurchaseRemovesProductFromStore() { - //Arrange + var products = getProductInList(TEST_PRODUCT_ID, "Product", 9.99, 10); + Store store = createStoreWithProducts(99.99, products); - //Act + store.Purchase(TEST_PRODUCT_ID, 9); - //Assert - //(choose the appropriate statement(s)) - //Assert.AreEqual(1, products[0].Quantity); - //Assert.AreSame(1, products[0].Quantity); - //Assert.IsTrue(products[0].Quantity == 1); + Assert.AreEqual(1, products[0].Quantity); } [Test] + [ExpectedException(typeof(InsufficientFundsException))] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { - //Arrange + var store = createStore(1.00, 1.01, 10); - //Act - - //Assert + store.Purchase(TEST_PRODUCT_ID, 1); } [Test] - public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() + [ExpectedException(typeof(OutOfStockException))] + public void Test_PurchaseThrowsOutOfStockException() { - //Arrange - - //Act + var store = createStore(99.99, 9.99, 1); - //Assert + store.Purchase(TEST_PRODUCT_ID, 2); } - // THE BELOW CODE IS REQUIRED TO PREVENT THE TESTS FROM MODIFYING THE USERS/PRODUCTS ON FILE // This is not a good unit testing pattern - the unit test dependency on the file system should // actually be broken ... training on how to do this will be coming. @@ -105,6 +119,8 @@ public void Test_Initialize() // Load products from data file originalProducts = JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Products.json")); + + } diff --git a/UnitTestProject/UnitTestProject.csproj b/UnitTestProject/UnitTestProject.csproj index bcf3847..1a9e337 100644 --- a/UnitTestProject/UnitTestProject.csproj +++ b/UnitTestProject/UnitTestProject.csproj @@ -70,6 +70,7 @@ +