Call base method OR override void method

980 views
Skip to first unread message

Rosemary Ehlers

unread,
Mar 14, 2014, 5:05:45 PM3/14/14
to nsubs...@googlegroups.com
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?

Rosemary Ehlers

unread,
Mar 14, 2014, 5:40:30 PM3/14/14
to nsubs...@googlegroups.com
Ahahaha, I answered my own question.

Because Save is virtual and the other methods are not, if I use Substitute.For<T> the virtual method will be overridden, but the other methods will persist.

D'oh.

David Tchepak

unread,
Mar 16, 2014, 7:09:47 PM3/16/14
to nsubs...@googlegroups.com
Glad you got it sorted out.

If all the methods were virtual then you should be able to substitute ForPartsOf<T>, then only replace the Save method (for example, using When..DoNotCallBase) so that the real implementations are called for the rest of the methods. There's a related example here: http://nsubstitute.github.io/help/partial-subs/

Regards,
David


--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nsubstitute...@googlegroups.com.
To post to this group, send email to nsubs...@googlegroups.com.
Visit this group at http://groups.google.com/group/nsubstitute.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages