Skip to content

Instantly share code, notes, and snippets.

@bharath2020
Last active February 22, 2016 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bharath2020/5df1d118b7a3005a8fe8 to your computer and use it in GitHub Desktop.
Save bharath2020/5df1d118b7a3005a8fe8 to your computer and use it in GitHub Desktop.
//test case #1
func testVMInvalidSelection{
//Prepare
let vendingMachine = VendingMachine()
var didReceiveInvalidSelectionError = false
//Execute
do{
try vendinMachine.vend("Coke")
}
catch VendingMachineError.InvalidSelection{
didReceiveInvalidSelectionError = true;
}
catch {
//Important to catch the failure cases, to make sure we are not receiving unexpected Errors
XCTAssertTrue(false, "Unexpected Exception \(error)")
}
//Verfiy
XCTAssertTrue(didReceiveInvalidSelectionError, "Expected Invalid Selection Error");
}
func testVMReturnCorrectNumberOfInsufficientCoinsError() {
//Preprare
let vendingMachine = VendingMachine()
var howManyCoins = 0
//Execute
do{
try vendingMachine.vend(itemNamed: "Candy Bar")
}
catch VendingMachineError.InsufficientFunds(let coinsNeeded) {
howManyCoins = coinsNeeded
}
catch {
//Important to catch the failure cases, to make sure we are not receiving unexpected Errors
XCTAssertTrue(false, "Unexpected Exception \(error)")
}
//Verify
XCTAssertTrue(howManyCoins==12, "Invalid number of coins needed")
}
func testVMInventoryUpdatedCorrectly(){
//Prepare
let vendingMachine = VendingMachine()
vendingMachine.coinsDeposited = 12
let item = vendingMachine.inventory["Candy Bar"]
let itemCountBeforeVend = item!.count
//Execute
do{
try vendingMachine.vend(itemNamed: "Candy Bar")
}
catch {
}
let itemAfterVend = vendingMachine.inventory["Candy Bar"]
let itemCountAfterVend = itemAfterVend!.count
//Verify
XCTAssertEqual(itemCountBeforeVend, itemCountAfterVend+1, "Invalid Item count before and after end")
//Since, the method relates to fetching items from inventory, Important to make sure that other details of the items are not tampered
XCTAssertEqual(itemBeforeVend.price , itemAfterVend.price, "Invalid Price before and after end")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment