From ad3bb0923fba2e471506ce7ed820de3f60044e4a Mon Sep 17 00:00:00 2001 From: xdong1 Date: Thu, 28 Apr 2016 12:53:52 -0500 Subject: [PATCH] Xiuli-UnitTest --- Refactoring/Store.cs | 2 +- UnitTestProject/AuthenticatorTests.cs | 37 ++++++++++++++ UnitTestProject/StoreTests.cs | 69 ++++++++++++++++++++------ UnitTestProject/UnitTestProject.csproj | 2 + 4 files changed, 93 insertions(+), 17 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..a126317 --- /dev/null +++ b/UnitTestProject/AuthenticatorTests.cs @@ -0,0 +1,37 @@ +using Newtonsoft.Json; +using NUnit.Framework; +using Refactoring; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +namespace UnitTestProject +{ + [TestFixture] + public 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_Authenticate() + { + //Arrange + var users = new List(); + users.Add(createTestUser("UnitTest", "UnitTest")); + + var authenticate = new Authenticator(users); + //Act + authenticate.Authenticate("UnitTest", string.Empty); + //Assert + Assert.IsFalse(users[0].Password ==string.Empty); + } + } +} diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 9f0b866..e480eaa 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -13,6 +13,7 @@ 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,21 +35,24 @@ private Product createTestProduct(string id, string name, double price, int quan return testProduct; } - [Test] - public void Test_PurchaseThrowsNoErrorForValidFunds() + private DataManager PreArrange(string userName, string userPassword, double userBalance, string productName, double productPrice, int productQty ) { - //Arrange - const string TEST_PRODUCT_ID = "1"; - var users = new List(); - users.Add(createTestUser("Test User", "", 99.99)); + users.Add(createTestUser(userName, userPassword, userBalance)); var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10)); - + products.Add(createTestProduct(TEST_PRODUCT_ID, productName, productPrice, productQty)); var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); + return dataManager; + } + + [Test] + public void Test_PurchaseThrowsNoErrorForValidFunds() + { + //Arrange + var dataManager = PreArrange("Test User", "", 99.99, "Product", 9.99, 10); + var store = new Store(dataManager.Users[0], dataManager); //Act store.Purchase(TEST_PRODUCT_ID, 10); @@ -60,12 +64,13 @@ public void Test_PurchaseThrowsNoErrorForValidFunds() public void Test_PurchaseRemovesProductFromStore() { //Arrange - + var dataManager = PreArrange("Test User", "", 99.99, "Product", 9.99, 10); + var store = new Store(dataManager.Users[0], dataManager); //Act - + store.Purchase(TEST_PRODUCT_ID, 9); //Assert //(choose the appropriate statement(s)) - //Assert.AreEqual(1, products[0].Quantity); + Assert.AreEqual(1, store.GetProductById(TEST_PRODUCT_ID).Quantity); //Assert.AreSame(1, products[0].Quantity); //Assert.IsTrue(products[0].Quantity == 1); } @@ -74,9 +79,17 @@ public void Test_PurchaseRemovesProductFromStore() public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { //Arrange - + var dataManager = PreArrange("Test User", "", 1.00, "Product", 1.01, 10); + var store = new Store(dataManager.Users[0], dataManager); //Act - + try + { + store.Purchase(TEST_PRODUCT_ID, 1); + } + catch(Exception e) + { + Assert.AreEqual("Exception of type 'Refactoring.InsufficientFundsException' was thrown.", e.Message); + } //Assert } @@ -84,12 +97,36 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() { //Arrange - + var dataManager = PreArrange("Test User", "", 1.00, "Product", 1.01, 10); + var store = new Store(dataManager.Users[0], dataManager); //Act - + double dBal = dataManager.Users[0].Balance - store.GetProductById(TEST_PRODUCT_ID).Price; //Assert + Assert.IsFalse(dBal > 0); } + [Test] + public void Test_StoreHasStockForPurchase() + { + //Arrange + var dataManager = PreArrange("Test User", "", 10.00, "Product", 0.55, 10); + var store = new Store(dataManager.Users[0], dataManager); + //Act + try + { + store.Purchase(TEST_PRODUCT_ID, 12); + } + catch (Exception e) + { + if (e.GetType().Name == typeof(OutOfStockException).Name) + Assert.IsFalse((store.GetProductById(TEST_PRODUCT_ID).Quantity - 12) > 0); + if ( e.GetType().Name ==typeof(InsufficientFundsException).Name) + Assert.IsFalse(dataManager.Users[0].Balance - store.GetProductById(TEST_PRODUCT_ID).Price * 12 > 0); + + } + //Assert + + } // 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 diff --git a/UnitTestProject/UnitTestProject.csproj b/UnitTestProject/UnitTestProject.csproj index bcf3847..c695f54 100644 --- a/UnitTestProject/UnitTestProject.csproj +++ b/UnitTestProject/UnitTestProject.csproj @@ -35,6 +35,7 @@ 4 + ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll True @@ -73,6 +74,7 @@ +