Skip to content

Instantly share code, notes, and snippets.

@JamesBender
Created October 18, 2013 16:43
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 JamesBender/7044286 to your computer and use it in GitHub Desktop.
Save JamesBender/7044286 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