Skip to content

Instantly share code, notes, and snippets.

@puleos
Created November 17, 2012 00:39
Show Gist options
  • Save puleos/4092208 to your computer and use it in GitHub Desktop.
Save puleos/4092208 to your computer and use it in GitHub Desktop.
Sample MSpec + Moq Unit Tests
using System;
using System.Collections.Generic;
using Machine.Specifications;
using Moq;
using NBTY.Core.DTO.Puritan;
using NBTY.Core.Enums;
using Puritan.Web.Services.Catalog;
using Puritan.Web.Services.Marketing;
using Puritan.Web.Services.Session;
using Puritan.Web.Services.ShoppingCart;
using It = Machine.Specifications.It;
namespace Puritan.Web.Services.Specs
{
public class ShoppingCartServiceSpecs
{
public class shopping_cart_context
{
protected static IShoppingCartService cartService;
protected static Mock<ICatalogService> catalogService;
protected static Mock<ITransactionRepository> webTransactionRepository;
protected static AddToCartAction AddAction;
protected const string FailureRoute = "epic/fail";
protected const string SuccessRoute = "for/the/win";
protected static decimal UnitPrice = new decimal(5.00);
protected static decimal SalePrice = new decimal(2.00);
protected static List<ItemCategory> ItemCategories;
Establish context = () =>
{
webTransactionRepository = new Mock<ITransactionRepository>();
session = new WebStoreUserSession() {SessionId = "SESSION_ID"};
AddAction = new AddToCartAction()
{
FailureRoute = FailureRoute,
LegacyCategoryId = 1,
ItemNumber = 5,
Quantity = 1,
SuccessRoute = SuccessRoute
};
catalogService = new Mock<ICatalogService>();
cartService = new ShoppingCartService(session, catalogService.Object, new Mock<IMarketingService>().Object,
webTransactionRepository.Object, new Mock<IPromotionShoppingCartManagerFactory>().Object, new Mock<ISessionRepository>().Object);
catalogService.Setup(x => x.GetCategory(1, false))
.Returns(new Category { LegacyId = 1 });
ItemCategories = new List<ItemCategory>
{
new ItemCategory {IsPrimary = true, LegacyId = 2, Name = "Primary"},
new ItemCategory {IsPrimary = false, LegacyId = 3, Name = "Secondary"}
};
catalogService.Setup(x => x.GetItemByItemNumber(5))
.Returns(new Item { ItemNumber = 5, Categories = ItemCategories,
Pricing = new ItemPricing {UnitPrice = UnitPrice, SaleType = ItemSalePriceTypeEnum.None, OfferCode = "3FOR1"}});
webTransactionRepository.Setup(x => x.UpsertQuote(Moq.It.IsAny<WebQuote>()))
.Returns((WebQuote q) => q);
webTransactionRepository.Setup(x => x.GetOrCreateQuote())
.Returns(new WebQuote());
};
static WebStoreUserSession session;
}
public class when_adding_an_invalid_item_to_the_cart : shopping_cart_context
{
Because b = () =>
{
AddAction.ItemNumber = 1;
result = cartService.AddToCart(AddAction);
};
It should_return_an_action_result_with_a_failure_route = () =>
result.ContinuationRoute.ShouldEqual(FailureRoute);
It should_return_an_action_result_success_flag_of_false = () =>
result.Success.ShouldBeFalse();
It should_return_an_action_result_of_invalid_item = () =>
result.Result.ShouldEqual(CartActionResultEnum.InvalidItem);
protected static AddToCartActionResult result;
}
public class when_adding_a_valid_item_to_the_cart_with_an_invalid_category : shopping_cart_context
{
Because b = () =>
{
AddAction.LegacyCategoryId = 10;
result = cartService.AddToCart(AddAction);
};
It should_return_an_action_result_with_the_primary_category_defined_for_the_item = () =>
result.Category.IsPrimary.ShouldBeTrue();
It should_return_an_action_result_with_a_success_route = () =>
result.ContinuationRoute.ShouldEqual(SuccessRoute);
It should_return_an_action_result_success_flag_of_true = () =>
result.Success.ShouldBeTrue();
It should_return_an_action_result_of_invalid_item = () =>
result.Result.ShouldEqual(CartActionResultEnum.Success);
protected static AddToCartActionResult result;
}
public class when_adding_an_item_to_the_cart : shopping_cart_context
{
Because b = () =>
{
result = cartService.AddToCart(AddAction);
};
It should_call_the_web_transaction_repo_GetOrCreateWebQuote_method = () =>
webTransactionRepository.Verify(x => x.GetOrCreateQuote());
It should_call_the_web_transaction_repo_upsert_quote_method = () =>
webTransactionRepository.Verify(x => x.UpsertQuote(Moq.It.IsAny<WebQuote>()));
It should_return_an_action_result_success_flag_of_true = () =>
result.Success.ShouldBeTrue();
It should_return_an_action_result_of_success = () =>
result.Result.ShouldEqual(CartActionResultEnum.Success);
protected static AddToCartActionResult result;
}
public class when_adding_an_item_exceeding_the_max_quantity : shopping_cart_context
{
Establish context = () =>
{
AddAction.Quantity = 100;
};
Because b = () =>
{
result = cartService.AddToCart(AddAction);
};
It should_limit_the_quantity_to_the_maximum = () =>
result.WebQuote.Items[AddAction.ItemNumber].BaseQuantity.ShouldEqual(50);
It should_return_an_action_result_success_flag_of_true = () =>
result.Success.ShouldBeTrue();
It should_return_an_action_result_of_max_limit_exceeded = () =>
result.Result.ShouldEqual(CartActionResultEnum.MaxLimitExceeded);
protected static AddToCartActionResult result;
}
public class when_adding_an_item_already_at_max_quantity : shopping_cart_context
{
Establish context = () =>
{
var quoteItems = new Dictionary<int, WebQuoteItem> {{5, new WebQuoteItem {BaseQuantity = 50, Item = new Item { ItemNumber = 5} }}};
webTransactionRepository.Setup(x => x.GetOrCreateQuote())
.Returns(new WebQuote {Items = quoteItems});
};
Because b = () =>
{
result = cartService.AddToCart(AddAction);
};
It should_return_an_action_result_with_a_failure_route = () =>
result.ContinuationRoute.ShouldEqual(FailureRoute);
It should_return_an_action_result_success_flag_of_false = () =>
result.Success.ShouldBeFalse();
It should_return_an_action_result_of_max_limit_exceeded = () =>
result.Result.ShouldEqual(CartActionResultEnum.MaxLimitExceeded);
protected static AddToCartActionResult result;
}
public class when_adding_a_1FOR1_base_price_item : shopping_cart_context
{
Establish context = () =>
{
catalogService.Setup(x => x.GetItemByItemNumber(5))
.Returns(new Item { ItemNumber = 5, Categories = ItemCategories,
Pricing = new ItemPricing { SalePrice = SalePrice,
SaleType = ItemSalePriceTypeEnum.None, OfferCode = "1FOR1" } });
};
Because b = () =>
{
result = cartService.AddToCart(AddAction);
};
It should_return_an_action_result_with_a_free_quantity_of_zero = () =>
result.WebQuote.Items[5].FreeQuantity.ShouldEqual(0);
It should_return_an_action_result_with_a_final_price_equal_to_the_unit_price_times_the_quantity = () =>
result.WebQuote.Items[5].FinalPrice.ShouldEqual(result.WebQuote.Items[5].Item.Pricing.SalePrice * result.WebQuote.Items[5].BaseQuantity);
protected static AddToCartActionResult result;
}
public class when_adding_a_3FOR1_item : shopping_cart_context
{
Establish context = () =>
{
catalogService.Setup(x => x.GetItemByItemNumber(5))
.Returns(new Item
{
ItemNumber = 5,
Categories = ItemCategories,
Pricing = new ItemPricing
{
UnitPrice = UnitPrice,
SalePrice = SalePrice,
SaleType = ItemSalePriceTypeEnum.None,
OfferCode = "3FOR1"
}
});
};
Because b = () =>
{
result = cartService.AddToCart(AddAction);
};
It should_return_an_action_result_with_a_free_quantity_of_two = () =>
result.WebQuote.Items[5].FreeQuantity.ShouldEqual(2);
It should_return_an_action_result_with_a_final_price_equal_to_the_unit_price_times_the_quantity = () =>
result.WebQuote.Items[5].FinalPrice.ShouldEqual(result.WebQuote.Items[5].Item.Pricing.SalePrice * result.WebQuote.Items[5].BaseQuantity);
protected static AddToCartActionResult result;
}
public class when_adding_a_2FOR1_base_price_item : shopping_cart_context
{
Establish context = () =>
{
catalogService.Setup(x => x.GetItemByItemNumber(5))
.Returns(new Item
{
ItemNumber = 5,
Categories = ItemCategories,
Pricing = new ItemPricing
{
UnitPrice = UnitPrice,
SaleType = ItemSalePriceTypeEnum.None,
OfferCode = "2FOR1"
}
});
catalogService.Setup(x => x.GetItemByItemNumber(10))
.Returns(new Item
{
ItemNumber = 10,
Categories = ItemCategories,
Pricing = new ItemPricing
{
UnitPrice = UnitPrice,
SaleType = ItemSalePriceTypeEnum.None,
OfferCode = "2FOR1"
}
});
};
Because b = () =>
{
result = cartService.AddToCart(AddAction);
AddAction.Quantity = 2;
AddAction.ItemNumber = 10;
result = cartService.AddToCart(AddAction);
};
It should_return_an_action_result_with_the_correct_free_quantity = () =>
{
result.WebQuote.Items[5].FreeQuantity.ShouldEqual(1);
result.WebQuote.Items[10].FreeQuantity.ShouldEqual(3);
};
protected static AddToCartActionResult result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment