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
Leave a Reply