I can't seem to figure out how to solve the following problem with NSubstitute:
I have a several public methods on a class, one of which is a void method called Save. When I mock an instance of this class, I want all methods besides Save to use the base implementation (since that is what I am testing), but Save makes the call to the database, and hence I want Save to do absolutely nothing. However:
If I use Substitute.For<T>, all my methods are overridden. If I use Substitue.ForPartsOf<T>, I have no way to override Save.
Example Code:
public class ExperimentVersion : IExperimentVersion
{
//omitted unrelated properties
public TimeSpan EnrollmentPeriod { get; private set; }
public DateTime Started { get; private set; }
public DateTime EndDate { get; private set; }
public virtual void Save()
{
//call to the database goes here
}
public void SetEndDate(DateTime endDate)
{
if (this.Started.Add(this.EnrollmentPeriod) > endDate)
this.EnrollmentPeriod = endDate.Subtract(this.Started);
this.EndDate = endDate;
this.Save();
}
public void SetEnrollmentEndDate(DateTime enrollmentEndDate)
{
if(enrollmentEndDate < this.Started)
throw new PlatformException("Enrollment cannot end before the version began.");
this.EnrollmentPeriod = enrollmentEndDate.Subtract(this.Started);
this.Save();
}
}
I want to be able to write tests that call SetEndDate and SetEnrollmentEndDate, but override Save so that I don't have the database dependency. Is this possible?