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 am looking at the use of NaturalMocks for some of our testing and was wondering how you can use NaturalMocks when you know that the return value will then be cast to another type.

As an example if I had a method Delete as shown below and the GetProvider method returns an object of type ProviderBase


public bool Delete()
{
   Provider dataProvider = ConnectionScope.Current.DataProvider;         
   OracleVWE_STB_BOOKMARKProvider providerInstance = dataProvider.GetProvider(typeof(OracleProvider)) as OracleProvider;
   bool result = providerInstance.Delete();
    return result;                     
}   


How would I use NaturalMocks to mock the GetProvider call so that it returns a Mock of type OracleProvider rather than of type ProviderBase.
asked by Cnash (2.3k points)

6 Answers

0 votes
You could try:

// get provider somehow (it could be mocked)
Provider dataProvider = ConnectionScope.Current.DataProvider;
// create fake OracleProvider
OracleProvider mockedProvider =  
   RecorderManager.CreateMockObject(typeof(OracleProvider));

using (RecordExpectations rec = RecorderManager.StartRecording())
{
   // return fake OracleProvider
   dataProvider.GetProvider(null);
   rec.Return(mockedProvider).RepeatAlways();

   // Delete must be called on our fake provider 
   mockedProvider.Delete();
   rec.Return(false); 
}
answered by scott (32k points)
0 votes
Thanks for the quick reply

I have tried doing the following

OracleProvider mockedProvider =
       RecorderManager.CreateMockedObject(typeof (OracleProvider)) as
            OracleProvider;

using (RecordExpectations recorder = RecorderManager.StartRecording())
{
   Provider dataProvider = ConnectionScope.Current.DataProvider;

   dataProvider.GetProvider(null);
   recorder.Return(mockedProvider);
   
   bool result = mockedProvider.Delete();
   recorder.Return(true);
}


However when the Delete method attempts to do:

OracleProvider providerInstance = dataProvider.GetProvider(typeof(OracleProvider)) as OracleProvider;


I get an exception thrown. Unexpected Call to MockProvider.GetProvider(). This confuses me a bit as surely the dataProvider.GetProvider(null) inside the recorder section means that it is expecting this call?

There is actually a little bit more surrounding this code so if you want me to post the full Delete method and Delete Test then let me know.

Thanks,
answered by Cnash (2.3k points)
0 votes
Please use the Tracer to see how the methods are called.
You might want to use: recorder.RepeatAlways();

If you need please export the trace log and send it to me via the support e-mail.
answered by scott (32k points)
0 votes
I've had a look at the tracer and can't see what the problem is as it shows it is expecting the call.

I cut the code down to the following test:

        [TestMethod]
        public void Provider_Problem_Test()
        {
            MockManager.Init();

            Mock MockOracleVWE_STB_BOOKMARKProvider = MockManager.MockObject(typeof (OracleVWE_STB_BOOKMARKProvider));
            OracleVWE_STB_BOOKMARKProvider ObjMockOracleVWE_STB_BOOKMARKProvider =
                (OracleVWE_STB_BOOKMARKProvider) MockOracleVWE_STB_BOOKMARKProvider.MockedInstance;            

            using (RecordExpectations recorder = RecorderManager.StartRecording())
            {
                EntityViewProviderAdapter temp = ConnectionScope.Current.DataProvider.GetProvider(null);
                recorder.Return(ObjMockOracleVWE_STB_BOOKMARKProvider);
            }

            try
            {
                OracleVWE_STB_BOOKMARKProvider provider =
                    ConnectionScope.Current.DataProvider.GetProvider(typeof (OracleVWE_STB_BOOKMARKProvider)) as OracleVWE_STB_BOOKMARKProvider;

                MockManager.Verify();
            }
            finally
            {
                MockManager.ClearAll();
            }
       }


I will send the details + trace file to the support e-mail now.

Thanks,
answered by Cnash (2.3k points)
0 votes
Hi Scott,

The only e-mails I can see on the contact page are info & sales. Where do you want me to send the trace file?

Thanks,
answered by Cnash (2.3k points)
0 votes
try: recorder.DefaultBehavior.RepeatAlways
I will send you the e-mail address off line
answered by scott (32k points)
...