Understanding Mock and frameworks – Part 4 of N
I am glad that this series has been liked by large audience and even got focus on Channel9 Video and that motivates to continue this series further on.
This series on Mock and frameworks takes you through Rhino Mocks, Moq and NSubstitute frameworks to implement mock and unit testing in your application. This series of posts is full of examples, code and illustrations to make it easier to follow. If you have not followed this series through, I would recommend you to read following articles
- Understanding Mock and frameworks – Part 1 of N – Understanding the need of TDD, Mock and getting your application Unit Test ready
- Understanding Mock and frameworks – Part 2 of N – Understanding Mock Stages & Frameworks – Rhino Mocks, NSubstitute and Moq
- Understanding Mock and frameworks – Part 3 of N - Understanding how to Mock Methods calls and their Output – Rhino Mocks, NSubstitute and Moq
This part of the series will focus on exception management, and subscribing to events
Exception Management
Going back to the second post where we defined our Controller and Service interface, the method GetObject throws an ArgumentNullException when a NULL value is passed to it. The Exception Management with Mock allows you to change the exception type and hence, ignore the exception peacefully.
Using Rhino Mocks
Using the Stub method of Mocked Service, you can override the exception type. So instead of getting a ArgumentNullException, we will change the exception to InvalidDataException. But do not forget to add a ExceptedException attribute to the method (line 2 in example below) that ensures a peaceful exit.
- [TestMethod]
- [ExpectedException(typeof(InvalidDataException))]
- publicvoid T05_TestExceptions()
- {
- var service = MockRepository.GenerateMock<IService>();
- // we are expecting that service will throw NULL exception, when null is passed
- service.Stub(x => x.GetObject(null)).Throw(newInvalidDataException());
- Assert.IsNull(service.GetObject(null)); // throws an exception
- }
Using NSubstitute
NSubstitute does not expose a separate /special function like Throw. It treats exceptions as a return value of the test method. So u
- [TestMethod]
- [ExpectedException(typeof(InvalidDataException))]
- publicvoid T05_TestExceptions()
- {
- var service = Substitute.For<IService>();
- // we are expecting that service will throw NULL exception, when null is passed
- service.GetObject(null).Returns(args => { thrownewInvalidDataException(); });
- Assert.IsNull(service.GetObject(null)); // throws an exception
- }
Using Moq
Moq exposes Throws method just like Rhino Mocks
- [TestMethod]
- [ExpectedException(typeof(InvalidDataException))]
- publicvoid T05_TestExceptions()
- {
- var service = newMock<IService>();
- // we are expecting that service will throw NULL exception, when null is passed
- service.Setup(x => x.GetObject(null)).Throws(newInvalidDataException());
- Assert.IsNull(service.Object.GetObject(null)); // throws an exception
- }
Event Subscriptions
The next topic of interest in this series is managing Event Subscriptions with Mock frameworks. So the target here is to check if an event was called when some methods were called on the mocked object
Using Rhino Mocks
The code here is little complex compared to all previous examples. So we are adding a ServiceObject to the controller which raises an event. Unlike real time examples, this event is hooked to the DataChanged method of the service. This means the controller will raise an event and a service method will be called dynamically.
- [TestMethod]
- publicvoid T06_TestSubscribingEvents()
- {
- var service = MockRepository.GenerateMock<IService>();
- var controller = newController(service);
- controller.CEvent += service.DataChanged;
- ServiceObject input =newServiceObject(“n1″, Guid.NewGuid());
- controller.Add(input);
- service.AssertWasCalled(x => x.DataChanged(Arg<Controller>.Is.Equal(controller),
- Arg<ChangedEventArgs>.Matches(m=> m.Action == Action.Add &&
- m.Data == input)));
- }
Let’s go into detail of the AssertWasCalled statement – it takes a method handler in the lambda expression. Some of the other methods of interest are Arg<T> Is.Equal and Matches. The method Arg<T> creates a fake argument object of a particular type. Is.Equal() method assigns a value to it and Matches method adds like a filter on the values of input parameters to the original ‘DataChanged’ method.
Using NSubstitute
NSubstitute has a different set of methods for event subscription. The output of Received() method exposes all valid method calls available, Arg.Is<T>() creates fake object for mocked service.
- [TestMethod]
- publicvoid T06_TestSubscribingEvents()
- {
- var service = Substitute.For<IService>();
- var controller = newController(service);
- controller.CEvent += service.DataChanged;
- ServiceObject input = newServiceObject(“n1″, Guid.NewGuid());
- controller.Add(input);
- service.Received().DataChanged(controller,
- Arg.Is<ChangedEventArgs>(m => m.Action == Action.Add && m.Data == input));
- }
Using Moq
Apart from having different names of methods from NSubstitute, Moq does exactly the same as other two frameworks
- publicvoid T06_TestSubscribingEvents()
- {
- var service = newMock<IService>();
- var controller = newController(service.Object);
- controller.CEvent += service.Object.DataChanged;
- ServiceObject input = newServiceObject(“n1″, Guid.NewGuid());
- controller.Add(input);
- service.Verify(x => x.DataChanged(controller,
- It.Is<ChangedEventArgs>(m => m.Action == Action.Add && m.Data == input)));
- }
The Code for Download
Understanding RhinoMocks, NSubstitute, Moq by Punit Ganshani
So that concludes how to use Rhino Mocks, NSubstitute and Moq for Exception Handling and Event Subscriptions. Should you have any questions, feel free to comment on this post.
Punit Ganshani
Punit Ganshani, based out of Singapore, specializes in Microsoft technology stack and performance engineering. He is an open-source contributor at Codeplex, has several apps on Windows Phone Store and is a noted speaker on Performance Tuning, Mobile Development and Application Design
Related posts:
- Understanding Mock and frameworks – Part 3 of N This series on Mock frameworks takes you through Rhino Mocks,...
- Understanding Mock and frameworks – Part 2 of N Continuing on where we left in the previous post –...
- Understanding Mock and frameworks – Part 1 of N We have several posts on the Internet that focus on...
- Intercepting method calls using IL Interception – The word interception is heard more in Canadian...
2 Responses to Understanding Mock and frameworks – Part 4 of N
Start Contributing!
If you have the knack of writing to share your knowledge with other spirited minds, you can register yourself and get started...
For more information, please visit our Contribute page
Register / Sign In
Information classified
- Announcements (1)
- Code (17)
- .NET (13)
- CSS and HTML (1)
- Database (1)
- Java (1)
- Javascript (1)
- Open Source (6)
- Web Services (1)
- Design (1)
- How-to (18)
- Office 365 (1)
- Server (4)
- SharePoint (8)
- TDD (4)
- Windows 8 (3)
- Presentations (1)
- Promotions (6)
- Uncategorized (1)
Latest on the Stack
- Complete guide to dynamic keyword in C#
- Sharing code: Adding NavigationService
- Mock driven development using .NET
- XecMe – Windows Service Host
- How to Write Access 2013 Custom Web App on Office 365
- Portable Class Library – Articles and Sample References
- Extension SDKs in Build Server without installing it!
- SharePoint 2013 and Search with REST API and ODATA
- Search Engine Changes in SharePoint 2013
- Consuming Odata Service in Windows Store Apps (Include MVVM Pattern)
Twitter Updates
- Mock driven development using .NET is.gd/GeHGLe 7 hours ago
- XecMe - Windows Service Host is.gd/0fjCQb #windowsservices #xecme 7 hours ago
- Complete guide to dynamic keyword in C# is.gd/Emmaju #net #development 11 hours ago
- Sharing code: Adding #NavigationService is.gd/0DDj9G #SharingCode #WindowsPhoneapp #WindowsStoreApps 20 hours ago
- Mock driven development using .NET is.gd/GeHGLe 1 day ago
Coming Soon















[...] this helps. In the next post, we will explore how to mock Exceptions and [...]
[...] Understanding Mock and frameworks – Part 4 of N - Understanding how to Mock Exceptions and Event Subscriptions – Rhino Mocks, NSubstitute and Moq [...]