From 2ae478738b300e6f79c807080367e861f846730a Mon Sep 17 00:00:00 2001 From: VarianErik Date: Thu, 28 Apr 2016 13:09:13 -0500 Subject: [PATCH] UnitTestTraining --- Refactoring/Store.cs | 2 +- UnitTestProject/AuthenticatorTests.cs | 59 ++++++++++++++ UnitTestProject/IntegrationTests.cs | 2 +- UnitTestProject/StoreTests.cs | 107 ++++++++++++++++++++----- UnitTestProject/UnitTestProject.csproj | 1 + 5 files changed, 151 insertions(+), 20 deletions(-) create mode 100644 UnitTestProject/AuthenticatorTests.cs 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..aee7642 --- /dev/null +++ b/UnitTestProject/AuthenticatorTests.cs @@ -0,0 +1,59 @@ +using Newtonsoft.Json; +using NUnit.Framework; +using Refactoring; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.Serialization.Formatters.Binary; + +namespace UnitTestProject +{ + [TestFixture] + class AuthenticatorTests + { + private User createTestUser(string name, string password) + { + User testUser = new User(); + testUser.Name = name; + testUser.Password = password; + + return testUser; + } + + [Test] + public void Test_AuthenticUser() + { + //Arrange + var testUser = new List(); + + testUser.Add(createTestUser("Frank", "pizza")); + var authenticate = new Authenticator( testUser ); + + //Act + var authenticUser = authenticate.Authenticate("Frank", "pizza"); + + //Assert + Assert.AreEqual(testUser[0], authenticUser); + + } + + [Test] + public void Test_NotValidUser() + { + //Arrange + var testUser = new List(); + + testUser.Add(createTestUser("Bill", "pizza")); + var authenticate = new Authenticator(testUser); + + //Act + var authenticUser = authenticate.Authenticate("", "pizza"); + + //Assert + Assert.AreNotEqual(testUser[0], authenticUser); + + } + + } +} 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..4b47ed7 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -13,6 +13,8 @@ namespace UnitTestProject [TestFixture] class StoreTests { + const string TEST_PRODUCT_ID = "1"; + private User createTestUser(string name, string password, double balance) { User testUser = new User(); @@ -34,38 +36,61 @@ private Product createTestProduct(string id, string name, double price, int quan return testProduct; } + private static Store SetupStore(List users, List products) + { + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); + return store; + } + + private List SetupProduct(double productPrice, int productQuantity) + { + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", productPrice, productQuantity)); + return products; + } + + private List SetupUser(double userBalance) + { + var users = new List(); + users.Add(createTestUser("Test User", "", userBalance)); + return users; + } + + [Test] public void Test_PurchaseThrowsNoErrorForValidFunds() { //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); - users.Add(createTestUser("Test User", "", 99.99)); + var users = SetupUser(99.99); - var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10)); + var products = SetupProduct(9.99, 10); - var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); + var store = SetupStore(users, products); //Act store.Purchase(TEST_PRODUCT_ID, 10); //Assert - Assert.Pass("No assertion really necessary here"); + Assert.Pass("No exception thrown."); } - + [Test] public void Test_PurchaseRemovesProductFromStore() { //Arrange + var users = SetupUser(99.99); + + var products = SetupProduct(9.99, 10); + + var store = SetupStore(users, products); //Act + store.Purchase(TEST_PRODUCT_ID, 9); //Assert //(choose the appropriate statement(s)) - //Assert.AreEqual(1, products[0].Quantity); + Assert.AreEqual(1, products[0].Quantity); //Assert.AreSame(1, products[0].Quantity); //Assert.IsTrue(products[0].Quantity == 1); } @@ -74,20 +99,66 @@ public void Test_PurchaseRemovesProductFromStore() public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { //Arrange - - //Act - - //Assert + var users = SetupUser(1.00); + + var products = SetupProduct(1.01, 10); + + var store = SetupStore(users, products); + + //Act / Assert + try + { + store.Purchase(TEST_PRODUCT_ID, 1); + Assert.Fail("Program did not throw the expected inssufficient funds exception"); + } + catch (InsufficientFundsException e) + { + Assert.Pass("Expected Exception thrown."); + } } [Test] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() { //Arrange + var users = SetupUser(2.00); + + var products = SetupProduct(1.01, 10); + + var store = SetupStore(users, products); + + //Act / Assert + try + { + store.Purchase(TEST_PRODUCT_ID, 2); + Assert.Fail("Program did not throw the expected inssufficient funds exception"); + } + catch (InsufficientFundsException e) + { + Assert.Pass("Expected Exception thrown."); + } + } - //Act - - //Assert + [Test] + public void Test_QuestFor100PercentCoverage() + { + //Arrange + var users = SetupUser(95.00); + + var products = SetupProduct(1.01, 1); + + var store = SetupStore(users, products); + + //Act / Assert + try + { + store.Purchase(TEST_PRODUCT_ID, 2); + Assert.Fail("Program did not throw the expected inssufficient funds exception"); + } + catch (OutOfStockException e) + { + Assert.Pass("Expected Exception thrown."); + } } 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 @@ +