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..b8a5362
--- /dev/null
+++ b/UnitTestProject/AuthenticatorTests.cs
@@ -0,0 +1,104 @@
+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
+{
+ ///
+ /// Summary description for AuthenticatorTests
+ ///
+ [TestFixture]
+ public class AuthenticatorTests
+ {
+
+ private User createTestUser(string name, string password, double balance)
+ {
+ User testUser = new User();
+ testUser.Name = name;
+ testUser.Password = password;
+ testUser.Balance = balance;
+
+ return testUser;
+ }
+
+ private Authenticator SetUpAuthenticatorAndTestUserList()
+ {
+ var users = new List();
+ users.Add(createTestUser("user1", "abcd", 99.99));
+ users.Add(createTestUser("user2", "1234", 9.99));
+ return new Authenticator(users);
+ }
+
+ [Test]
+ public void Test_AuthenticateReturnsUserOnSuccess()
+ {
+ //Arrange
+ var authenticator = SetUpAuthenticatorAndTestUserList();
+ //Act
+ var returnedValue = authenticator.Authenticate("user1", "abcd");
+ //Assert
+ Assert.IsInstanceOf(returnedValue);
+ }
+
+ [Test]
+ public void Test_AuthenticateReturnsNULLOnBadPassword()
+ {
+ //Arrange
+ var authenticator = SetUpAuthenticatorAndTestUserList();
+ //Act
+ var returnedValue = authenticator.Authenticate("user1", "ab34");
+ //Assert
+ Assert.IsNull(returnedValue);
+ }
+
+ [Test]
+ public void Test_AuthenticateReturnsNULLOnBadUsername()
+ {
+ //Arrange
+ var authenticator = SetUpAuthenticatorAndTestUserList();
+ //Act
+ var returnedValue = authenticator.Authenticate("user3", "abcd");
+ //Assert
+ Assert.IsNull(returnedValue);
+ }
+
+
+
+
+ // 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.
+ private List originalUsers;
+ private List originalProducts;
+
+ [SetUp]
+ public void Test_Initialize()
+ {
+ // Load users from data file
+ originalUsers = JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Users.json"));
+
+ // Load products from data file
+ originalProducts = JsonConvert.DeserializeObject>(File.ReadAllText(@"Data/Products.json"));
+ }
+
+
+
+ [TearDown]
+ public void Test_Cleanup()
+ {
+ // Restore users
+ string json = JsonConvert.SerializeObject(originalUsers, Formatting.Indented);
+ File.WriteAllText(@"Data/Users.json", json);
+
+ // Restore products
+ string json2 = JsonConvert.SerializeObject(originalProducts, Formatting.Indented);
+ File.WriteAllText(@"Data/Products.json", json2);
+ }
+ }
+}
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..9a3e1ce 100644
--- a/UnitTestProject/StoreTests.cs
+++ b/UnitTestProject/StoreTests.cs
@@ -34,20 +34,29 @@ private Product createTestProduct(string id, string name, double price, int quan
return testProduct;
}
- [Test]
- public void Test_PurchaseThrowsNoErrorForValidFunds()
+ private Store purchaseMethodSetupSingleUserSingleProduct(double balance, string id, double price, int quantity)
{
- //Arrange
- const string TEST_PRODUCT_ID = "1";
-
var users = new List();
- users.Add(createTestUser("Test User", "", 99.99));
+ users.Add(createTestUser("Test User", "", balance));
var products = new List();
- products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10));
+ products.Add(createTestProduct(id, "Product", price, quantity));
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
+
+ const string TEST_PRODUCT_ID = "1";
+ var store = purchaseMethodSetupSingleUserSingleProduct(99.99,TEST_PRODUCT_ID, 9.99, 10);
+
//Act
store.Purchase(TEST_PRODUCT_ID, 10);
@@ -60,34 +69,66 @@ public void Test_PurchaseThrowsNoErrorForValidFunds()
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
+ const string TEST_PRODUCT_ID = "1";
+ var store = purchaseMethodSetupSingleUserSingleProduct(99.99, TEST_PRODUCT_ID, 9.99, 10);
//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, store.GetProductById(TEST_PRODUCT_ID).Quantity);
+ //Assert.AreSame(1, store.GetProductById(TEST_PRODUCT_ID).Quantity);
+ Assert.IsTrue(store.GetProductById(TEST_PRODUCT_ID).Quantity == 1);
}
[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
{
//Arrange
-
+ const string TEST_PRODUCT_ID = "1";
+ var store = purchaseMethodSetupSingleUserSingleProduct(1.00, TEST_PRODUCT_ID, 1.01, 10);
//Act
-
+ try
+ {
+ store.Purchase(TEST_PRODUCT_ID, 1);
+ }
//Assert
+ catch(InsufficientFundsException ife)
+ {
+ Assert.IsTrue(true);
+ }
+
+ Assert.Fail();
+
}
[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2()
{
//Arrange
+ const string TEST_PRODUCT_ID = "1";
+ var store = purchaseMethodSetupSingleUserSingleProduct(1.00, TEST_PRODUCT_ID, 1.01, 10);
+ //Act
+ //Assert
+ Assert.That(() => store.Purchase(TEST_PRODUCT_ID, 1),
+ Throws.Exception
+ .TypeOf());
+ }
+
+ [Test]
+ public void Test_PurchaseThrowsExceptionWhenQuantityTooLow()
+ {
+ //Arrange
+ const string TEST_PRODUCT_ID = "1";
+ var store = purchaseMethodSetupSingleUserSingleProduct(99.99, TEST_PRODUCT_ID, 9.99, 1);
//Act
//Assert
+ Assert.That(() => store.Purchase(TEST_PRODUCT_ID, 2),
+ Throws.Exception
+ .TypeOf());
}
@@ -108,6 +149,7 @@ public void Test_Initialize()
}
+
[TearDown]
public void Test_Cleanup()
{
diff --git a/UnitTestProject/UnitTestProject.csproj b/UnitTestProject/UnitTestProject.csproj
index bcf3847..81e158c 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
@@ -70,6 +71,7 @@
+