It’s a very rare requirement, but sometimes in .NET you have to create your own primitive and make it behave as close as possible to a native CTS (common type system) type. “That shouldn’t be hard” would be your first thought, until you start considering all the scenarios in which it could be used. Continue reading “Creating your own primitive type”
Unit Testing – Setup pattern (Moq)
In my previous post I mentioned that large unit test setups can be difficult to maintain / understand. This problem can be reduced by following a test pattern. I want to share with you a pattern I use, and I think works really well. In this post I will be using C#, Moq libary and Visual Studio test tools.
To start here are some things I believe make a good suite of unit tests:
- Easy to identify data dependencies of tests.
- Mocking is not repeated every test.
- Test are clear and easy to understand / maintain.
- New tests can be added with relative ease.
In order to explain this testing pattern I have forked my buddies TodoMVC app, and introduced a service layer, repository pattern and test project. You can see my source code with an example test class on github.
The TodoModule (service layer) has a number of public methods; Get…
View original post 315 more words
Object Oriented, Test Driven Design in C# and Java: A Practical Example Part #1
For a brief overview, please refer to this post.
At this point, many tutorials start by launching into a “Hello, World” style tutorial, with very little practical basis.
This isn’t the most exciting concept, so let’s try a more practical example. Instead of churning out boring pleasantries, our application is going to do something a bit more interesting…build robots.
Specifically, our application is going to build awesome robots with big guns.
Ok, let’s get started with a narrative description of what we’re going to do.
“Mechs with Big Guns” is a factory that produces large, robotic vehicles designed to shoot other large, robotic vehicles. Robots are composed of several robotic parts, delivered by suppliers. Parts are loaded into a delivery bay, and are transported by worker drones to various rooms; functional parts such as arms, legs, etc., are dispatched to an assembly room. Guns…
View original post 876 more words
Extending mocking with Moq
In the last part of my Dependency Injection article I introduced the term of “mocking”. This kind of test double can be really powerful. Yet in my example I had to create 2 new classes (my mocks) to be able to test my functionality in order to reduced coupling. Here is the code used by the tests:
class MockNotifier : INotifier {public MockNotifier(){ NotifyHasBeenCalled =false;} publicbool NotifyHasBeenCalled {get;privateset;} publicvoid Notify(User user){ NotifyHasBeenCalled =true;}} class MockRepository : IUserRepository {publicbool HasValidatedNotification {get;set;} public User GetById(int userId){returnnew User { HasActivatedNotification = HasValidatedNotification };}} [TestClass]publicclass NotificationServiceTest {private NotificationService _notificationService;private MockNotifier _mockNotifier;private MockRepository _mockRepository
View original post 659 more words