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;
}
}