I have a method CreateGraph(DateTime) that takes a long time to execute.
I don't want to call it if not needed, hence, it the input parameter is not changed, I do not want to call CreateGraph, but return the already created Graph:
IGraphFactor GraphFactory {get; set;}
DateTime LastCreatedGraphDate {get; private set;}
IGraph LastCreatedGraph {get; private set;}
IGraph FetchGraph(DateTime graphDate)
{
if (graphDate != LastCreatedGraph && this.LastCreatedGraph != null)
{
this.LastCreatedGraph = this.GraphFactory.CreateGraph(graphDate);
}
return this.LastCreatedGraph;
}
Simple comme bonjour.
Now I want to test it using Rhino Mocks. First FetchGraph for some testDate, then Fetch it again and assert that the re-fetch does not call GraphFactory.CreateGraph again:
Using the Arrange - Act - Assert pattern:
// Arrange:
IGraph mockedGraph = MockRepository.GenerateMock<IGraph>();
IGraphBuilder mockedGraphBuilder MockRepository.GenerateMock<IGraphBuilder>();
// mockedGraphBuilder.CreateGraph returns a mockedGraph:
mockedGraphBuilder.Stub( (graphBuilder) => graphBuilder.CreateGraph(Arg<DateTime>.Is.Anything))
.Return(mockedGraph);
DateTime testDate = ...
var myTestObject = ...
myTestObject.FetchGraph(testDate);
// This will probably have called GraphBuilder.CreateGraph.
// I Need to reset this recording before Act
// how to reset?
// now that I know that LastCreatedGraphDate equals testDate I can act:
// Act: fetch the graph for the same date again:
myTestObject.FetchGraph(testDate);
// Assert: should not be called
mockedGraphBuilder.AssertNotCalled((graphBuilder) => graphBuilder.CreateGraph(Arg<DateTime>.Is.Anything))
This fails, because the first call was recorded.
To reset I tried: mockedGraphBuilder.Replay, and mockedGraphBuilder.BackToRecord but that does not work. How to reset the recording, so that it is as if the method is not called yet?