Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Refactoring/Store.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
59 changes: 59 additions & 0 deletions UnitTestProject/AuthenticatorTests.cs
Original file line number Diff line number Diff line change
@@ -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<User>();

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<User>();

testUser.Add(createTestUser("Bill", "pizza"));
var authenticate = new Authenticator(testUser);

//Act
var authenticUser = authenticate.Authenticate("", "pizza");

//Assert
Assert.AreNotEqual(testUser[0], authenticUser);

}

}
}
2 changes: 1 addition & 1 deletion UnitTestProject/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace UnitTestProject
{
[TestFixture]
//[Ignore("Disable integration tests")]
[Ignore("Disable integration tests")]
public class IntegrationTests
{
private List<User> users;
Expand Down
107 changes: 89 additions & 18 deletions UnitTestProject/StoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -34,38 +36,61 @@ private Product createTestProduct(string id, string name, double price, int quan
return testProduct;
}

private static Store SetupStore(List<User> users, List<Product> products)
{
var dataManager = new DataManager(users, products);
var store = new Store(users[0], dataManager);
return store;
}

private List<Product> SetupProduct(double productPrice, int productQuantity)
{
var products = new List<Product>();
products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", productPrice, productQuantity));
return products;
}

private List<User> SetupUser(double userBalance)
{
var users = new List<User>();
users.Add(createTestUser("Test User", "", userBalance));
return users;
}


[Test]
public void Test_PurchaseThrowsNoErrorForValidFunds()
{
//Arrange
const string TEST_PRODUCT_ID = "1";

var users = new List<User>();
users.Add(createTestUser("Test User", "", 99.99));
var users = SetupUser(99.99);

var products = new List<Product>();
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);
}
Expand All @@ -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.");
}
}


Expand Down
1 change: 1 addition & 0 deletions UnitTestProject/UnitTestProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="AuthenticatorTests.cs" />
<Compile Include="StoreTests.cs" />
<Compile Include="IntegrationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down