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?
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.
Trying to mock a method that is called within another method. // code part public virtual bool hello(string name, int age) { string lastName = GetLastName(); } public virtual string GetLastName() { return "xxx"; } // unit test part Mock<Program> p = new Mock<Program>(); p. Setup(x => x. GetLastName()).
Make a protected getter for this private variable, and override it in testing subclass to return a mock object instead of the actual private variable. Create a protected factory method for creating ISnapshot object, and override it in testing subclass to return an instance of a mock object instead of the real one.
Moq, how it works The idea is to create a concrete implementation of an interface and control how certain methods on that interface responds when called. This will allow us to essentially test all of the paths through code.
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;
}
}
Seems awfully/overly complicated to put a SetupGet inside SetupSet implementation.
It'd be easier to have the .Returns return a delegate, so it is evaluated everytime, instead of just returning a copy of the reference.
Something like this is a lot easier on the eye (and should perform better as you're not continually re-defining the getter).
SessionData
.SetupSet(s => s.TheReport = It.IsAny<Report>())
.Callback<RDLDesigner.Common.Report>(r => _sessionReport = r);
SessionData
.SetupGet(s => s.TheReport).Returns(() => _sessionReport);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With