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
I have a test where I am substituting a call to my DAL class with a call to a stub. Both return an IDataReader which is used internal to by business object.

Isolate.WhenCalled(() => fakeDal.GetItemNumbersFromProduction()).WillReturn(new SingleItemNumbersStub().GetDataReaderStub());


When testing my ModelView code, the GetItemNumbersFromProduction is called twice. Once upon creation of the ModelView, and again after a save operation (other methods on the fakeDal are also called as part of the process of creating the MV). After the Save is where I am seeing a problem in that it appears that the IDataReader returned from the above code is cached, and again used for the second call to the faked method. However at this point the data reader is closed and so an exception is thrown.

Is there a way to use Isolater such that every time the fakeDal.GetItemNumbersFromProduction() method is called, the WillReturn is re-evaluated, causing a new data reader to be returned?

Thanks.
Fintan
asked by fvanomme (1.6k points)

1 Answer

0 votes
I have a test where I am substituting a call to my DAL class with a call to a stub. Both return an IDataReader which is used internal to by business object.

Isolate.WhenCalled(() => fakeDal.GetItemNumbersFromProduction()).WillReturn(new SingleItemNumbersStub().GetDataReaderStub());


When testing my ModelView code, the GetItemNumbersFromProduction is called twice. Once upon creation of the ModelView, and again after a save operation (other methods on the fakeDal are also called as part of the process of creating the MV). After the Save is where I am seeing a problem in that it appears that the IDataReader returned from the above code is cached, and again used for the second call to the faked method. However at this point the data reader is closed and so an exception is thrown.

Is there a way to use Isolater such that every time the fakeDal.GetItemNumbersFromProduction() method is called, the WillReturn is re-evaluated, causing a new data reader to be returned?


In answering my own question. It appears that the following code will do what I am after.

Isolate.WhenCalled(() => fakeDal.GetItemNumbersFromProduction()).DoInstead(callContext => { return new SingleItemNumbersStub().GetDataReaderStub(); });


Is this the correct approach?
answered by fvanomme (1.6k points)
...