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
0 votes
TypeMock is such a cool product! I am evaluating it and have stumbled onto the first thing I have not figured out how to do with it.

I have a parent business class that in turn contains a collection of child classes that are exposed by a property on the parent. Both the parent data and child data are populated as a result of calling the factory method of the parent. I would like to mock the access to the database and the trouble I am having is figuring out how to return a different result for the SqlDataReader in the parent class than the SqlDataReader in the child collection.

Is there some way to mock up an ExpectAndReturn only if SqlCommand.CommandText in one DataReader is "FetchParent" and another ExpectAndReturn if the SqlCommand.CommandText is "FetchChildren"?
asked by dasryte (600 points)

1 Answer

0 votes
Hi,

What you are looking for is called "conditional expectations". Based on the value of arguments sent to the method, you want to set the behavior - return differnet values or even decide to mock or call the original method.

Here's the example, taken from the TypeMock Help file (which you should look up under "Conditional Expectations for Natural TypeMock"):
[Test] public void Conditional ()
{
   using (RecordExpectations recorder = RecorderManager.StartRecording())
   {
     // Always return TypeMock for great_mocking_framework key
     DateBase.GetValueForKey("great_mocking_framework"); 
     recorder.Return("TypeMock").RepeatAlways().WhenArgumentsMatch();
     // Always return Natural for easy_way key
     DateBase.GetValueForKey("easy_way"); 
     recorder.Return("Natural").RepeatAlways().WhenArgumentsMatch();
   }
} 


Let me know if you need additional assitance.
answered by gilz (14.5k points)
...