Skip to content

Instantly share code, notes, and snippets.

@akshitzaveri
Created April 23, 2020 18:36
Show Gist options
  • Save akshitzaveri/26302ed6b3d0e43bbb843690e0233f41 to your computer and use it in GitHub Desktop.
Save akshitzaveri/26302ed6b3d0e43bbb843690e0233f41 to your computer and use it in GitHub Desktop.
func test_WhenProductIsNotNilAndAddToCartIsCalled_ThenTheProductIsAdded() {
// given
self.viewModel.product = Product(id: 1, name: "Grey T-Shirt", pricePerUnit: 20)
let exp = self.expectation(description: "Waiting for async operation")
// when
self.viewModel.addToCart { (result) in
// then
switch result {
case .success:
print("Passed")
// Reading the products from the cart to verify the addition of the product
self.viewModel.getProductsFromCart { (products) in
// There should be exactly 1 product in the cart
XCTAssertEqual(products.count, 1)
// Comparing the product details
let product = products.first!
XCTAssertEqual(product.id, 1)
XCTAssertEqual(product.name, "Grey T-Shirt")
XCTAssertEqual(product.pricePerUnit, 20)
exp.fulfill()
}
default: XCTFail()
}
}
self.waitForExpectations(timeout: 10, handler: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment