public void Initialize() //---- moq initializer. { _good = new MealProduct("Milk", 2, 100); _goodsList = new List <AbstractGood> { new Clothes("C1", 2, 23), new MealProduct("C2", 1, 1123), new Clothes("C3", 7, 213) }; _bigPrice = 0; _goodsList.ToList <AbstractGood>().ForEach(i => _bigPrice += i.Price); _repositoryMoq = new Mock <IRepository <AbstractGood> >(); //---- setup result of register product _repositoryMoq.Register() in repository. _repositoryMoq.Setup(r => r.Register(It.IsAny <AbstractGood>())) .Callback <AbstractGood>(u => u.ID = int.MinValue); //---- setup result of unregister product _repositoryMoq.Register() in repository. _repositoryMoq.Setup(r => r.Unregister(It.IsAny <int>())) .Callback(() => _good = null); //---- setup result of update product _repositoryMoq.Update() in repository. _repositoryMoq.Setup(r => r.Update(It.IsAny <AbstractGood>())) .Callback(() => _good = new MealProduct("Cheese", 5, 100) { ID = _good.ID }); //---- setup result of _repositoryMoq.GetProduct(int id) call. _repositoryMoq.Setup(r => r.GetProduct(It.IsAny <int>())) .Returns(_good); //---- setup result of _repositoryMoq.GetProductByName(string name) call. _repositoryMoq.Setup(r => r.GetProductByName(It.IsAny <string>())) .Returns(_good); //---- setup result of _repositoryMoq.GetShopList(string name) call. _repositoryMoq.Setup(r => r.GetShopList()) .Returns(_goodsList); //---- setup result of _repositoryMoq.GetShopList(string name) call. _repositoryMoq.Setup(r => r.MakePurchase(It.IsAny <AbstractGood>())) .Returns(_good.Price); //---- setup changing user as null in _repositoryMoq.Delete() method. _repositoryMoq.Setup(r => r.MakePurchases(It.IsAny <IEnumerable <AbstractGood> >())) .Callback(() => _goodsList = null) .Returns(_bigPrice); }
/// <summary> /// Product verification method. /// </summary> /// <param name="product">Product under test.</param> public static void ProductValidation(AbstractGood product) { //validation var results = new List <ValidationResult>(); var context = new ValidationContext(product); if (!Validator.TryValidateObject(product, context, results, true)) { string exceptionMessage = $"Product {product} validation exception:\n "; int i = 1; foreach (var error in results) { exceptionMessage = string.Concat(exceptionMessage, $"{i++}) {error.ErrorMessage}\n"); } throw new ValidationException(exceptionMessage); } }