Using Moq callbacks to check complex arguments sent to a dependency - Online Free Computer Tutorials.

'Software Development, Games Development, Mobile Development, iOS Development, Android Development, Window Phone Development. Dot Net, Window Services,WCF Services, Web Services, MVC, MySQL, SQL Server and Oracle Tutorials, Articles and their Resources

Tuesday, August 7, 2018

Using Moq callbacks to check complex arguments sent to a dependency

I've written before about the various ways you can test the contents of a complex parameter sent to a dependency. The messages you see in RhinoMocks and Moq when a test fails aren't always clear. You can use Moq callbacks to give clear test failure messages. Consider the system under test. Your creating an order, and using some of the data in the order object to create a customer. Obviously, you would have written this code test-driven – the aim of the test would be to ensure the customer object is created properly. public class OrderService { private readonly ICustomerService _customerService; public OrderService(ICustomerService customerService) { _customerService = customerService; } public void CreateOrder(Order order, int createdBy) { var customer = new Customer { FirstName = order.FirstName, LastName = order.FirstName //intentional bug }; _customerService.Save(customer, createdBy); //TODO: do some other stuff with the order. } } In a test, let's just use Moq's Verify method and you'll see what I mean about clear failure messages: [Test] public void WhenCreatingAnOrderSaveTheCustomerDetails() { const int orderCreatedByPersonId = 1; var customerServiceMock = new Mock(); var orderService = new OrderService(customerServiceMock.


I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


This article is related to

.NET,Moq,Test Driven Development,TDD

No comments:

Post a Comment