Mocks: the whisky example

It’s pretty half-baked, but a colleague was interested in a simple example:

mockWhisky01 mockWhisky02 mockWhisky03 mockWhisky04 mockWhisky05

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
 
namespace ProductApplication.UnitTest {
    [TestClass]
    public class MockTests {
 
        const string Product = "Talisker";
        const int RequiredQty = 5;
 
        [TestMethod]
        public void OrderCanBeMetForTheRequestedQuantityAndStockIsDecremented() {
 
            
            var warehouse = new Mock<IWarehouse>(MockBehavior.Strict);
            warehouse.Setup(x => x.HasStock(It.IsAny<string>(), It.IsAny<int>())).Returns(true);
            warehouse.Setup(x => x.ReduceStock(It.IsAny<string>(), It.IsAny<int>()));
 
            var o = new Order(Product, warehouse.Object);
 
            bool orderPlaced = o.PlaceOrder(RequiredQty);
            warehouse.VerifyAll();
 
            Assert.AreEqual(true, orderPlaced);
 
        }
 
        [TestMethod]
        public void ProductHasInsufficientStockAtWarehouse() {
 
            var warehouse = new Mock<IWarehouse>(MockBehavior.Strict);
            warehouse.Setup(x => x.HasStock(It.IsAny<string>(), It.IsAny<int>())).Returns(false);
 
            var o = new Order(Product, warehouse.Object);
 
            bool orderPlaced = o.PlaceOrder(RequiredQty);
            warehouse.Verify(x => x.ReduceStock(It.IsAny<string>(), It.IsAny<int>()), Times.Never());
 
            Assert.AreEqual(false, orderPlaced);
 
        }
    }
}




namespace ProductApplication {
    class Program {
        static void Main(string[] args) {
        }
    }
}

using System;
 
namespace ProductApplication {
 
    /// <summary>
    /// Manages an order
    /// </summary>
    public class Order {
        private readonly string _product;
        private readonly IWarehouse _warehouse;
 
 
        public Order(string product, IWarehouse warehouse) {
            if (warehouse == null) {
                throw new ArgumentException("warehouse is null");
            }
            if (product == null) {
                throw new ArgumentException("product is null");
            }
            _warehouse = warehouse;
            _product = product;
        }
 
        /// <summary>
        /// Places the order if the warehouse has enough stock for the current product.
        /// Decrements the stock accordingly.
        /// </summary>
        /// <param name="quantity">units of stock required</param>
        /// <returns>true if the warehouse has enough stock, else false</returns>
        public bool PlaceOrder(int quantity) {
            bool placed = false;
 
            if (_warehouse.HasStock(_product, quantity)) {
                _warehouse.ReduceStock(_product, quantity);
                placed = true;
            }
 
            return placed;
        }
    }
}

Snippet
namespace ProductApplication {
 
    /// <summary>
    /// A warehouse, with a focus on stock levels
    /// </summary>
    public interface IWarehouse {
        bool HasStock(string product, int amount);
        void ReduceStock(string product, int amount);
    }
}



Moq “QuickStart”, which is not a QuickStart, it’s more of a cheat sheet. I think that given this…

mockWhisky08

, you at least need this to illustrate if you are starting out:

mockWhisky06

 mockWhisky07
Advertisements