Extending mocking with Moq

Coding Journeyman


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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: