tags:

views:

47

answers:

1

Using Moq I am mocking a property, Report TheReport { get; set; } on an interface ISessionData so that I can inspect the value that gets set on this property.

To achieve this I'm using SetupGet and SetupSet as follows:

// class-level fields
protected Report _sessionReport;
protected Mock<ISessionData> SessionData { get; private set; }

And in my setup method...

SessionData = new Mock<ISessionData>();

SessionData
    .SetupSet(s => s.TheReport = It.IsAny<Report>())
    .Callback<RDLDesigner.Common.Report>(r =>
    {
        _sessionReport = r;
        SessionData.SetupGet(s => s.TheReport).Returns(_sessionReport);
    });

I found this approach on StackOverflow and it works, but I do not understand why. I expected to have the call to SetupGet outside of the SetupSet callback.

Can anyone explain how and why this approach works, and if it is the most appropriate way of mocking a property of this type?

Edit

Using SessionData.SetupProperty(s => s.TheReport); also works in my scenario, but I am still interested in any explanations for how and why my original approach worked.

+2  A: 

The reason why the callback is used in the call to SetupGet is that the _sessionReport reference is passed by value, this means that a subsequent call to the Set method would not update the value returned by the get method.

To see what's going on more clearly. If you had setup your Mock as follows:-

SessionData.SetupSet(s => s.Report = It.IsAny<Report>());
SessionData.SetupGet(s => s.Report).Returns(_report);

In pseudocode the Mocked implementation will look a little like

public Report Report {
    set { }
    get { 
       // Copy of the value of the _report reference field in your test class
       return _reportCopy; 
    }  
}

So doing something like this wouldn't work:-

 ISessionData session = SessionData.Object
 Report report = new Report();
 session.Report = report;
 session.Report.ShouldEqual(report); //Fails
 _report.ShouldEqual(report); // Fails

Obviously we need to add some behaviour to the Set method so we set up the Mock like so

SessionData.SetupSet(s => s.Report = It.IsAny<Report>())
           .Callback(s => _report = s);
SessionData.SetupGet(s => s.Report).Returns(_report);

This leads to the Mocked implementation looking a little like

public Report Report {
    set {
       // Invokes delegate that sets the field on test class
    }
    get { 
       // Copy of the original value of the _report reference field
       // in your test class
       return _reportCopy; 
    }  
}

However this leads to the following problem:-

  ISessionData session = SessionData.Object
  Report report = new Report();
  session.Report = report;
  _report.ShouldEqual(report); // Passes
  session.Report.ShouldEqual(report); //Fails!

In essence the "get" method on the property still returns a reference to the original object _report was pointing to as the reference was passed by value to the SetupGet method.

We therefore need to update the value the report getter returns every time the setter is called which leads to your original code

SessionData
   .SetupSet(s => s.TheReport = It.IsAny<Report>())
   .Callback<RDLDesigner.Common.Report>(r => {
        _sessionReport = r;
        SessionData.SetupGet(s => s.TheReport).Returns(_sessionReport);
   });

This ensures that the value returned by the Get method is always kept in sync with the previous call to the set method. And leads to something that (functionally) behaves like:-

public Report Report {
    set {
       // Sets the field on the test class
       _reportCopy = value;
    }
    get { 
       // Copy of the value of the _report reference field in your test class
       return _reportCopy; 
    }  
}
sighohwell