chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi, new time user to Typemock and I'm running into a problem I suspect a seasoned user can answer in relatively no time.

I have a simple database object that has 3 fields (a GUID ID, startDate, and endDate) and I'm trying to test a static method that creates a new object if the current date is not in that range.

The problem is when I call the Save() method I can't access the properties of the instance for the DoInstead() stub. Here is the testmethod:

[TestMethod, Isolated]
public void StatementPeriod_GetStatementPeriod_ReturnsNewPeriod_LastMicroSecondInDecember()
// Make sure calls to DateTime.Now always return the last time before New Year! (~12/31/2012 12:59.99pm)
Isolate.WhenCalled(() => DateTime.Now).WillReturn(new DateTime(2013, 1, 1).AddTicks(-1));

// Create a new statement period that will be used for the next "new" StatementPeriod.
var fakeStatementPeriod = Isolate.Fake.Instance<StatementPeriod>();

// Pretend we don't have an existing one yet for some reason.
Isolate.WhenCalled(() => StatementPeriod.LoadCurrent()).WillReturn(null);
// We don't want these database methods to actually hit the database.
Isolate.WhenCalled(() => fakeStatementPeriod.Save()).DoInstead(s => { Logging.WriteLog(fakeStatementPeriod.ToString(), false); });

// Call the method we're testing to make sure it actually returns a new statement period for 12/1/2012 to 1/1/2013.
StatementPeriod statement = StatementPeriod.GetStatementPeriod();

// Make sure the start and end dates are correct.
Assert.AreEqual<DateTime>(new DateTime(2012, 12, 1), statement.StartDate, "START DATE IS INCORRECT.");
Assert.AreEqual<DateTime>(new DateTime(2013, 1, 1), statement.EndDate, "END DATE IS INCORRECT.");

/* Here is the method that's being tested. */

public static StatementPeriod GetStatementPeriod()
StatementPeriod statementPeriod = StatementPeriod.LoadCurrent();

if (statementPeriod == null)
// NOTE: The assumption is this is being run more or less continuously and 4+ weeks haven't passed by.
// If that is the case, you'll have to add the dates in manually.
// Calculate the new date
int startMonth = (DateTime.Now.Month); // Use the current month.
int startYear = (DateTime.Now.Year); // Use the current year.
int endMonth = (startMonth + 1) % 12; // There's no 13th month, so go back to 1 if the current month is December.
int endYear = (endMonth < startMonth) ? startYear + 1 : startYear; // If the end month is January, we have to make it next year.
DateTime startDate = new DateTime(startYear, startMonth, 1);
DateTime endDate = new DateTime(endYear, endMonth, 1);

// Add a new third party ledger for this particular combination of issuerID, entrySetup
statementPeriod = new StatementPeriod() { StatementPeriodID = Guid.NewGuid(), StartDate = startDate, EndDate = endDate };

return statementPeriod;

Thanks in advance.
asked by LIONHEART76 (600 points)

1 Answer

0 votes

If I understand correctly (correct me if I'm wrong), you're facing the next issue:
The problem is when I call the Save() method I can't access the properties of the instance for the DoInstead() stub

In order to access the instance in DoInstead() you should use the method's context the like this:
Isolate.WhenCalled(() => fakeStatementPeriod.Save())
         .DoInstead( context => { 
             var start = ((StatementPeriod)context.Instance).StartDate  ;

  • 1)Get the instance using the context.
    2)Convert it to the relevant type.
    3)State the property you'd like to change.
answered by alex (17k points)