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..a654d72 --- /dev/null +++ b/UnitTestProject/AuthenticatorTests.cs @@ -0,0 +1,85 @@ +using System; +using NUnit.Framework; +using Refactoring; +using System.Collections.Generic; + +namespace UnitTestProject +{ + [TestFixture] + public class AuthenticatorTests + { + List users; + Authenticator authenticator; + + private User createTestUser(string name, string password, double balance) + { + User testUser = new User(); + testUser.Name = name; + testUser.Password = password; + testUser.Balance = balance; + + return testUser; + } + + [SetUp] + public void Test_CodeSetup() + { + users = new List(); + users.Add(createTestUser("Test User A", "pass123", 99.99)); + users.Add(createTestUser("Test User B", "Password01", 99.99)); + users.Add(createTestUser("Test User C", "password", 99.99)); + users.Add(createTestUser("Test User D", "hunter2", 99.99)); + users.Add(createTestUser("Test User E", "f8W&3M9v=", 99.99)); + + authenticator = new Authenticator(users); + } + + [Test] + public void Test_NullOrEmptyUsername() + { + User result; + result = authenticator.Authenticate("", "password"); + Assert.AreEqual(null, result); + } + + [Test] + public void Test_ValidUsername() + { + User result; + result = authenticator.Authenticate("Test User D", "hunter2"); + Assert.AreEqual(users[3], result); + } + + [Test] + public void Test_CannotFindUserWithIncorrectUsername() + { + User result; + result = authenticator.Authenticate("Test User X", "pass123"); + Assert.AreEqual(null, result); + } + + [Test] + public void Test_CannotFindUserWithIncorrectPassword() + { + User result; + result = authenticator.Authenticate("Test User C", "nopass"); + Assert.AreEqual(null, result); + } + + [Test] + public void Test_FindUserByCredentials() + { + User result; + result = authenticator.Authenticate("Test User C", "password"); + Assert.AreEqual(users[2], result); + } + + [Test] + public void Test_FindUserByCredentialsNullPassword() + { + User result; + result = authenticator.Authenticate("Test User E", null); + Assert.AreEqual(users[4], result); + } + } +} diff --git a/UnitTestProject/StoreTests.cs b/UnitTestProject/StoreTests.cs index 9f0b866..9cabcc9 100644 --- a/UnitTestProject/StoreTests.cs +++ b/UnitTestProject/StoreTests.cs @@ -13,6 +13,12 @@ namespace UnitTestProject [TestFixture] class StoreTests { + const string TEST_PRODUCT_ID = "1"; + List users; + List products; + DataManager dataManager; + Store store; + private User createTestUser(string name, string password, double balance) { User testUser = new User(); @@ -34,60 +40,85 @@ private Product createTestProduct(string id, string name, double price, int quan return testProduct; } - [Test] - public void Test_PurchaseThrowsNoErrorForValidFunds() + private Store createStore(List users, List products) { - //Arrange - const string TEST_PRODUCT_ID = "1"; + DataManager dataManager = new DataManager(users, products); + Store store = new Store(users[0], dataManager); + return store; + } - var users = new List(); + [SetUp] + public void Test_CodeSetup() + { + // Set up variables here for use when testing. Subsequent tests will alter the specfics of this basic configuration for their individual tests + users = new List(); users.Add(createTestUser("Test User", "", 99.99)); - var products = new List(); + 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 + [Test] + public void Test_PurchaseThrowsNoErrorForValidFunds() + { + store = createStore(users, products); store.Purchase(TEST_PRODUCT_ID, 10); - - //Assert Assert.Pass("No assertion really necessary here"); } [Test] public void Test_PurchaseRemovesProductFromStore() { - //Arrange - - //Act - - //Assert - //(choose the appropriate statement(s)) - //Assert.AreEqual(1, products[0].Quantity); - //Assert.AreSame(1, products[0].Quantity); - //Assert.IsTrue(products[0].Quantity == 1); + store = createStore(users, products); + store.Purchase(TEST_PRODUCT_ID, 9); + Assert.AreEqual(1, products[0].Quantity); + //Assert.IsTrue(products[0].Quantity == 1); Functional and correct, but the first assert is more appropriate } [Test] + [ExpectedException(typeof(InsufficientFundsException))] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow() { - //Arrange + users[0].Balance = 1.00; + products[0].Price = 1.01; - //Act + store = createStore(users, products); - //Assert + store.Purchase(TEST_PRODUCT_ID, 1); + Assert.Fail("InsufficientFundsException was not thrown"); // Should not reach this point due to the [ExpectedException] attribute on this test } [Test] public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2() { - //Arrange + users[0].Balance = 1.00; + products[0].Price = 1.01; + + store = createStore(users, products); + + try + { + store.Purchase(TEST_PRODUCT_ID, 1); + Assert.Fail("InsufficientFundsException was not thrown"); // Should not reach this point due to an invalid balance + } + catch (InsufficientFundsException ex) + { + Assert.True(ex is InsufficientFundsException); + } + } + + + [Test] + [ExpectedException(typeof(OutOfStockException))] + public void Test_ProductOutOfStock() + { + users[0].Balance = 100.00; + products[0].Price = 5; - //Act + store = createStore(users, products); - //Assert + store.Purchase(TEST_PRODUCT_ID, 12); + Assert.Fail("OutOfStockException was not thrown"); // Should not reach this point due to the [ExpectedException] attribute on this test } 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 @@ +