From 8ed0e2cb7024d4f5e15840137ee358554708336a Mon Sep 17 00:00:00 2001 From: ecassell Date: Fri, 29 Apr 2016 14:18:10 -0500 Subject: [PATCH 1/5] Unit Testing - Exercise #1 --- Refactoring/Store.cs | 2 +- UnitTestProject/IntegrationTests.cs | 2 +- UnitTestProject/StoreTests.cs | 12 +++++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) 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/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..c63a652 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -60,14 +60,24 @@ public void Test_PurchaseThrowsNoErrorForValidFunds() public void Test_PurchaseRemovesProductFromStore() { //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 dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); //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.IsTrue(products[0].Quantity == 1); } [Test] From 02b8f1cac69bd6a1743a593d266aa0d689916b1e Mon Sep 17 00:00:00 2001 From: ecassell Date: Fri, 29 Apr 2016 14:42:55 -0500 Subject: [PATCH 2/5] Basic Unit Testing - Exercise #2 --- UnitTestProject/StoreTests.cs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index c63a652..02073c1 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -81,23 +81,45 @@ public void Test_PurchaseRemovesProductFromStore() } [Test] + [ExpectedException(typeof(InsufficientFundsException))] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { //Arrange + const string TEST_PRODUCT_ID = "1"; + + var users = new List(); + users.Add(createTestUser("Test User", "", 1.00)); + + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 10)); + + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); //Act + store.Purchase(TEST_PRODUCT_ID, 1); //Assert + Assert.Fail("InsufficientFundsException should have been thrown."); } [Test] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() { //Arrange + const string TEST_PRODUCT_ID = "1"; - //Act + var users = new List(); + users.Add(createTestUser("Test User", "", 1.00)); - //Assert + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 10)); + + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); + + //Act / Assert + Assert.Throws(() => store.Purchase(TEST_PRODUCT_ID, 1)); } From 0eb18e90e6f9b66334284000a06751dbd9ae5d93 Mon Sep 17 00:00:00 2001 From: ecassell Date: Fri, 29 Apr 2016 14:46:24 -0500 Subject: [PATCH 3/5] Basic Unit Testing - Exercise #3 --- UnitTestProject/StoreTests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 02073c1..33fae9b 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -122,6 +122,24 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() Assert.Throws(() => store.Purchase(TEST_PRODUCT_ID, 1)); } + [Test] + public void Test_PurchaseThrowsExceptionWhenStockIsTooLow() + { + //Arrange + const string TEST_PRODUCT_ID = "1"; + + var users = new List(); + users.Add(createTestUser("Test User", "", 10.00)); + + var products = new List(); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.00, 1)); + + var dataManager = new DataManager(users, products); + var store = new Store(users[0], dataManager); + + //Act / Assert + Assert.Throws(() => 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 From 775e5cdbf7a4bbe7a91b26c3c7902dd82e56ed58 Mon Sep 17 00:00:00 2001 From: ecassell Date: Fri, 29 Apr 2016 15:34:02 -0500 Subject: [PATCH 4/5] Basic Unit Testing - Exercise #4 --- UnitTestProject/StoreTests.cs | 58 ++++++++++------------------------- 1 file changed, 17 insertions(+), 41 deletions(-) diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 33fae9b..6153290 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -13,6 +13,8 @@ namespace UnitTestProject [TestFixture] class StoreTests { + private readonly string TEST_PRODUCT_ID = "1"; + private User createTestUser(string name, string password, double balance) { User testUser = new User(); @@ -34,20 +36,23 @@ private Product createTestProduct(string id, string name, double price, int quan return testProduct; } - [Test] - public void Test_PurchaseThrowsNoErrorForValidFunds() + private Store createTestStoreWithSingleUserAndProduct(double userBalance, double productPrice, int productQuantity) { - //Arrange - const string TEST_PRODUCT_ID = "1"; - var users = new List(); - users.Add(createTestUser("Test User", "", 99.99)); + users.Add(createTestUser("Test User", "", userBalance)); var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10)); + products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", productPrice, productQuantity)); var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); + + return new Store(users[0], dataManager); + } + [Test] + public void Test_PurchaseThrowsNoErrorForValidFunds() + { + //Arrange + var store = createTestStoreWithSingleUserAndProduct(99.99, 9.99, 10); //Act store.Purchase(TEST_PRODUCT_ID, 10); @@ -60,8 +65,6 @@ public void Test_PurchaseThrowsNoErrorForValidFunds() public void Test_PurchaseRemovesProductFromStore() { //Arrange - const string TEST_PRODUCT_ID = "1"; - var users = new List(); users.Add(createTestUser("Test User", "", 99.99)); @@ -85,17 +88,8 @@ public void Test_PurchaseRemovesProductFromStore() public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); - users.Add(createTestUser("Test User", "", 1.00)); - - var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 10)); - - var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); - + var store = createTestStoreWithSingleUserAndProduct(1.00, 1.01, 10); + //Act store.Purchase(TEST_PRODUCT_ID, 1); @@ -107,16 +101,7 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() { //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); - users.Add(createTestUser("Test User", "", 1.00)); - - var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.01, 10)); - - var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); + var store = createTestStoreWithSingleUserAndProduct(1.00, 1.01, 10); //Act / Assert Assert.Throws(() => store.Purchase(TEST_PRODUCT_ID, 1)); @@ -126,16 +111,7 @@ public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() public void Test_PurchaseThrowsExceptionWhenStockIsTooLow() { //Arrange - const string TEST_PRODUCT_ID = "1"; - - var users = new List(); - users.Add(createTestUser("Test User", "", 10.00)); - - var products = new List(); - products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 1.00, 1)); - - var dataManager = new DataManager(users, products); - var store = new Store(users[0], dataManager); + var store = createTestStoreWithSingleUserAndProduct(10.00, 1.00, 1); //Act / Assert Assert.Throws(() => store.Purchase(TEST_PRODUCT_ID, 2)); From 6277d5bbc54ac08c6b06696dfb90e32d98cde0f8 Mon Sep 17 00:00:00 2001 From: ecassell Date: Fri, 29 Apr 2016 15:50:43 -0500 Subject: [PATCH 5/5] Basic Unit Testing - Exercise #5 --- UnitTestProject/AuthenticatorTests.cs | 113 +++++++++++++++++++++++++ UnitTestProject/UnitTestProject.csproj | 1 + 2 files changed, 114 insertions(+) create mode 100644 UnitTestProject/AuthenticatorTests.cs diff --git a/UnitTestProject/AuthenticatorTests.cs b/UnitTestProject/AuthenticatorTests.cs new file mode 100644 index 0000000..f06ca11 --- /dev/null +++ b/UnitTestProject/AuthenticatorTests.cs @@ -0,0 +1,113 @@ +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] + class AuthenticatorTests + { + private List users; + + [TestFixtureSetUp] + public void Class_Initialize() + { + users = new List(); + users.Add(createTestUser("Test1", "", 10)); + users.Add(createTestUser("Test2", "t2", 20)); + } + + private User createTestUser(string name, string password, double balance) + { + User testUser = new User(); + testUser.Name = name; + testUser.Password = password; + testUser.Balance = balance; + + return testUser; + } + + [Test] + public void Test_InitializeWithoutError() + { + //Arrange + //Act + Authenticator authenticator = new Authenticator(users); + + //Assert + Assert.Pass("No assertion really necessary here"); + } + + [Test] + public void Test_AuthenticateUserSuccessfully() + { + //Arrange + Authenticator authenticator = new Authenticator(users); + + //Act + User user = authenticator.Authenticate("Test1", ""); + + //Assert + Assert.IsTrue(user.Name == "Test1"); + } + + [Test] + public void Test_AuthenticationFailedIfUsernameIncorrect() + { + //Arrange + Authenticator authenticator = new Authenticator(users); + + //Act + User user = authenticator.Authenticate("Test3", ""); + + //Assert + Assert.IsTrue(user == null); + } + + [Test] + public void Test_AuthenticateUserSuccessfullyWithCorrectPassword() + { + //Arrange + Authenticator authenticator = new Authenticator(users); + + //Act + User user = authenticator.Authenticate("Test2", "t2"); + + //Assert + Assert.IsTrue(user.Name == "Test2"); + } + + [Test] + public void Test_AuthenticationFailedIfPasswordIncorrect() + { + //Arrange + Authenticator authenticator = new Authenticator(users); + + //Act + User user = authenticator.Authenticate("Test2", "test22"); + + //Assert + Assert.IsTrue(user == null); + } + + [Test] + public void Test_AuthenticationFailedIfUsernameNull() + { + //Arrange + Authenticator authenticator = new Authenticator(users); + + //Act + User user = authenticator.Authenticate(null, ""); + + //Assert + Assert.IsTrue(user == null); + } + + } +} 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 @@ +