How to mock lazy initialization of objects in C# unit tests using Moq

I was unable to find it clearly documented how to use Moq with objects that are to be lazy initialized, so below are the steps I took to do so.

Here is my class under test:

public class MyClass
{
  [Dependency]
  public Lazy<IWorkflowService> WorkflowService { get; set; }

  [Dependency]
  public Lazy<ISession> Session { get; set; }

  public string MyPublicMethod()
  {
    var someValue = this.WorkflowService.Value.GetSomeValue(this.Session.Value.SessionId);

    //Do something with someValue...
    return someValue;
  }
}

Notice that the GetSomeValue method requires the SessionId off of the Session object.

I now needed to write a unit test for MyPublicMethod().  The question I had was how to actually use Moq with the Session and WorkflowService instances.  Normally, I would just be able to do this in my test:

var mockSession = new Mock<ISession>();
var mockWorkfowService = new Mock<IWorkflowService>(); 

var myClass = new MyClass(); 
myClass.Session = mockSession.Object;
myClass.WorkflowService = mockWorkfowService.Object; 
//Method under test
var actual = myClass.MyPublicMethod();

But when I run the test, I received this error:

System.NullReferenceException: Object reference not set to an instance of an object.

The lazy initialization meant I needed to take another approach.  I read the MSDN Lazy Initialization  documentation and noticed there is a Lazy class constructor that allows you to specify a Func that should be used to return an instance of the object.  I decided to take advantage of this in my test case and return the mock instances that I had already created:

var mockSession = new Mock<ISession>();
var mockWorkfowService = new Mock<IWorkflowService>();

var myClass = new MyClass();

//Make use of the Lazy<T>(Func<T>()) constructor 
//to return the mock instances
myClass.Session = new Lazy<ISession>( () => mockSession.Object);
myClass.WorkflowService = 
    new Lazy<IWorkflowService>( () => mockWorkfowService.Object); 

//Method under test
var actual = myClass.MyPublicMethod();

Re-ran the test and it passed!

 

Advertisements

About stevewall123

I am a Lead Software Engineer in Minneapolis working for Thomson Reuters. I am currently working on projects using Java, JavaScript, Spring, Drools, Hazelcast, Liquibase and Tomcat. Previously, I used C#, GWT, Grails, Groovy, JMS and JBoss Drools Guvnor. In the past I have worked on projects using J2EE, Swing, Webwork, Hibernate, Spring, Spring-WS, JMS, JUnit and Ant.
This entry was posted in C#, Moq and tagged , . Bookmark the permalink.

2 Responses to How to mock lazy initialization of objects in C# unit tests using Moq

  1. Mikah Trent says:

    This was helpful – thanks for the post, glad I found it.

  2. Candis says:

    Was helpful 🙂

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 )

Google+ photo

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

Connecting to %s