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
7 changes: 4 additions & 3 deletions Refactoring.sln
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tusc", "Refactoring\Tusc.csproj", "{2D3F5D0E-4A6B-44C0-8F63-8E95243AC028}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestProject", "UnitTestProject\UnitTestProject.csproj", "{1FAF99A1-CCBF-435D-B599-4DAE1DAD5105}"
Expand All @@ -25,4 +23,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal
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
52 changes: 52 additions & 0 deletions UnitTestProject/AuthenticatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Newtonsoft.Json;
using NUnit.Framework;
using Refactoring;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnitTestProject
{
[TestFixture]
class AuthenticatorTests
{
private User testUser;
private Authenticator authenticator;

[Test]
public void Test_Authenticate_ShouldSucceed()
{
User user = authenticator.Authenticate("Chris", "");
Assert.AreEqual(testUser, user);
}

[Test]
public void Test_Authenticate_InvalidPassword()
{
User user = authenticator.Authenticate("Chris", "asdf");
Assert.Null(user);
}

[Test]
public void Test_Authenticate_NullUserName()
{
User user = authenticator.Authenticate(null, "");
Assert.Null(user);
}

[SetUp]
public void Test_Initialize()
{
List<User> users = new List<User>();
testUser = new User();
testUser.Name = "Chris";
testUser.Password = "";
testUser.Balance = 100;
users.Add(testUser);

authenticator = new Authenticator(users);
}
}
}
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
76 changes: 46 additions & 30 deletions UnitTestProject/StoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,38 @@ namespace UnitTestProject
[TestFixture]
class StoreTests
{
private const String TEST_PRODUCT_ID = "1";

private Store createStore(double balance, double product_cost, int quantity)
{
var users = getUserInList("Test User", "", balance);
var products = getProductInList(TEST_PRODUCT_ID, "Product", product_cost, quantity);

var dataManager = new DataManager(users, products);
return new Store(users[0], dataManager);
}

private Store createStoreWithProducts(double balance, List<Product> products)
{
var users = getUserInList("Test User", "", balance);
var dataManager = new DataManager(users, products);
return new Store(users[0], dataManager);
}

private List<User> getUserInList(string name, string password, double balance)
{
var users = new List<User>();
users.Add(createTestUser("Test User", "", balance));
return users;
}

private List<Product> getProductInList(string id, string name, double price, int quantity)
{
var products = new List<Product>();
products.Add(createTestProduct(id, "Product", price, quantity));
return products;
}

private User createTestUser(string name, string password, double balance)
{
User testUser = new User();
Expand All @@ -37,60 +69,42 @@ private Product createTestProduct(string id, string name, double price, int quan
[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 products = new List<Product>();
products.Add(createTestProduct(TEST_PRODUCT_ID, "Product", 9.99, 10));
var store = createStore(99.9, 9, 10);

var dataManager = new DataManager(users, products);
var store = new Store(users[0], dataManager);

//Act
store.Purchase(TEST_PRODUCT_ID, 10);

//Assert
Assert.Pass("No assertion really necessary here");
}

[Test]
public void Test_PurchaseRemovesProductFromStore()
{
//Arrange
var products = getProductInList(TEST_PRODUCT_ID, "Product", 9.99, 10);
Store store = createStoreWithProducts(99.99, products);

//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, products[0].Quantity);
}

[Test]
[ExpectedException(typeof(InsufficientFundsException))]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLow()
{
//Arrange
var store = createStore(1.00, 1.01, 10);

//Act

//Assert
store.Purchase(TEST_PRODUCT_ID, 1);
}

[Test]
public void Test_PurchaseThrowsExceptionWhenBalanceIsTooLowVersion2()
[ExpectedException(typeof(OutOfStockException))]
public void Test_PurchaseThrowsOutOfStockException()
{
//Arrange

//Act
var store = createStore(99.99, 9.99, 1);

//Assert
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
// actually be broken ... training on how to do this will be coming.
Expand All @@ -105,6 +119,8 @@ public void Test_Initialize()

// Load products from data file
originalProducts = JsonConvert.DeserializeObject<List<Product>>(File.ReadAllText(@"Data/Products.json"));


}


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