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>();
Isolate.Swap.NextInstance<StatementPeriod>().With(fakeStatementPeriod);
// 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 };
statementPeriod.Save();
}
return statementPeriod;
}
Thanks in advance.