1 / 9

Using Moq

Using Moq. by Jon Kruger. What Moq does. Creates “fake” objects for you that you can use in tests. Code!. Auto-mocking containers. Helps us create the class under test and all of its dependencies. public class OrderProcessor {

bernie
Download Presentation

Using Moq

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Moq by Jon Kruger

  2. What Moq does • Creates “fake” objects for you that you can use in tests

  3. Code!

  4. Auto-mocking containers • Helps us create the class under test and all of its dependencies

  5. public class OrderProcessor { private readonlyIGetObjectService<Order> _getOrderService; private readonlyITaxCalculator _taxCalculator; private readonlyIShippingCalculator _shippingCalculator; public OrderProcessor(IGetObjectService<Order> getOrderService, ITaxCalculatortaxCalculator, IShippingCalculatorshippingCalculator) { _getOrderService = getOrderService; _taxCalculator = taxCalculator; _shippingCalculator = shippingCalculator; } public decimal CalculateTotalPrice(intorderId) { // load order from database var order = _getOrderService.Get(orderId); vartotalPriceOfAllProducts = order.TotalPriceOfAllProducts; // calculate tax decimal tax = _taxCalculator.CalculateTax(order); // calculate shipping decimal shippingCharges = _shippingCalculator.CalculateShipping(order); return totalPriceOfAllProducts + tax + shippingCharges; } }

  6. Auto-mocking containers public class OrderProcessorTests { private OrderProcessor _orderProcessor; [SetUp] public void Setup() { var mocker = new MoqAutoMocker<OrderProcessor>(); vartaxCalculator = mocker.Get<ITaxCalculator>(); Mock.Get(taxCalculator).Setup(s => s.CalculateTax(_order)).Returns(1.5m); _orderProcessor = mocker.ClassUnderTest; } } Call mocker.Get<T>() to get a stub of a class that will be injected into the constructor of the class under test.

  7. Auto-mocking containers public class OrderProcessorTests { private OrderProcessor _orderProcessor; [SetUp] public void Setup() { var mocker = new MoqAutoMocker<OrderProcessor>(); vartaxCalculator = mocker.Get<ITaxCalculator>(); Mock.Get(taxCalculator).Setup(s => s.CalculateTax(_order)).Returns(1.5m); _orderProcessor = mocker.ClassUnderTest; } } Call mocker.ClassUnderTest to get the class under test. Do this last.

  8. Auto-mocking containers public class OrderProcessorTests { private OrderProcessor _orderProcessor; [SetUp] protected void Setup() { var mocker = new MoqAutoMocker<OrderProcessor>(); mocker.Inject<ITaxCalculator>(new CustomFakeTaxCalculator()); _orderProcessor =mocker.ClassUnderTest; } } Call mocker.Inject<T>() to inject your own class into the constructor of the class under test.

  9. ?

More Related