Skip to content

Instantly share code, notes, and snippets.

@Hameds
Created November 2, 2017 10:38
Show Gist options
  • Save Hameds/357e6bd138c9c50c9e078e07d936ba43 to your computer and use it in GitHub Desktop.
Save Hameds/357e6bd138c9c50c9e078e07d936ba43 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using NUnit.Framework;
using TddStore.Core;
using TddStore.Core.Exceptions;
using Telerik.JustMock;
namespace TddStore.UnitTests
{
[TestFixture]
class OrderServiceTests
{
private OrderService _orderService;
private IOrderDataService _orderDataService;
private ICustomerService _customerService;
[TestFixtureSetUp]
public void SetupTestFixture()
{
_orderDataService = Mock.Create<IOrderDataService>();
_customerService = Mock.Create<ICustomerService>();
_orderService = new OrderService(_orderDataService, _customerService);
}
[Test]
public void WhenUserPlacesACorrectOrderThenAnOrderNumberShouldBeReturned()
{
//Arrange
var shoppingCart = new ShoppingCart();
shoppingCart.Items.Add(new ShoppingCartItem { ItemId = Guid.NewGuid(), Quantity = 0 });
var customerId = Guid.NewGuid();
var expectedOrderId = Guid.NewGuid();
Mock.Arrange(() => _orderDataService.Save(Arg.IsAny<Order>()))
.Returns(expectedOrderId)
.OccursOnce();
//Act
var result = _orderService.PlaceOrder(customerId, shoppingCart);
//Assert
Assert.AreEqual(expectedOrderId, result);
Mock.Assert(_orderDataService);
}
[Test]
public void WhenAUserAttemptsToOrderAnItemWithAQuantityOfZeroThrowInvalidOrderException()
{
//Arrange
var shoppingCart = new ShoppingCart();
shoppingCart.Items.Add(new ShoppingCartItem { ItemId = Guid.NewGuid(), Quantity = 0 });
var customerId = Guid.NewGuid();
var expectedOrderId = Guid.NewGuid();
Mock.Arrange(() => _orderDataService.Save(Arg.IsAny<Order>()))
.Returns(expectedOrderId)
.OccursNever();
//Act
try
{
_orderService.PlaceOrder(customerId, shoppingCart);
}
catch(InvalidOrderException ex)
{
//Assert
Mock.Assert(_orderDataService);
Assert.Pass();
}
//Assert
Assert.Fail();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment