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
This is similar to an existing bug which was already fixed, but in this case instead of having the default behavior be .RepeatAlways() we are putting a set number of repeats on the item.

For example:

recorder.ExpectAndReturn(HostingEnvironment.VirtualPathProvider.FileExists(resourceToReturn), true).Repeat(2);


Will work on the first call, and then return a failure on the second call unless we explicitly put another line, with the same code in it below the first call.

The error returned is:

TestCase 'Configuration.ConfigurationManager.ManagerRetrieved_XmlDocument'
failed: TypeMock.VerifyException : 
TypeMock Verification: Unexpected Call to System.Web.Hosting.VirtualPathProvider.FileExists()
   at TypeMock.Mock.a(String A_0, Object[] A_1, Object A_2, Object A_3, Int32 A_4, Boolean& A_5, String A_6)
   at ax.a(String A_0, Object[] A_1, Object A_2, Object A_3, String A_4, Boolean& A_5)
   at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
   at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Object p1)
   at System.Web.Hosting.VirtualPathProvider.FileExists(String virtualPath)
   C:devtk-14853_ConfigManagersolutionFrameworkConfigurationConfigurationManager.cs(1689,0): at Corillian.Olb.Framework.Configuration.ConfigurationManager.GetConfigXmlDocumentFromResource(String resourcePath)
   C:devtk-14853_ConfigManagersolutionFrameworkConfigurationConfigurationManager.cs(946,0): at ConfigurationManager.GetConfigurationManager(String resourcePath)
   C:devtk-14853_ConfigManagersolutionTestUnitFrameworkConfigurationConfigurationManager.cs(140,0): at Configuration.ConfigurationManager.ManagerRetrieved_XmlDocument()
   at TypeMock.MethodDecorator.CallRealMethod()
   at TypeMock.DecoratorAttribute.CallDecoratedMethod()
   at TypeMock.VerifyMocksAttribute.Execute()
   at TypeMock.MethodDecorator.f()
   at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
   at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
   C:devtk-14853_ConfigManagersolutionTestUnitFrameworkConfigurationConfigurationManager.cs(136,0): at Configuration.ConfigurationManager.ManagerRetrieved_XmlDocument()


Here's the entirety of the code I'm using to mock the VirtualPathProvider if it helps at all:

VirtualFile mockVirtualFile = RecorderManager.CreateMockedObject<VirtualFile>(Constructor.Mocked);
         CacheDependency mockCacheDependency = RecorderManager.CreateMockedObject<CacheDependency>(Constructor.Mocked);
         Stream dataStream = null;

         if (resourceToReturn != null)
         {
            dataStream = GetResourceStringFromAssembly(resourceToReturn);
         }


         using (RecordExpectations recorder = RecorderManager.StartRecording())
         {
            recorder.ExpectAndReturn(HostingEnvironment.VirtualPathProvider.FileExists(resourceToReturn), fileExists).Repeat(2); // FAILS 

[b]// If we do the following code then it works:
//recorder.ExpectAndReturn(HostingEnvironment.VirtualPathProvider.FileExists(resourceToReturn), fileExists)
//recorder.ExpectAndReturn(HostingEnvironment.VirtualPathProvider.FileExists(resourceToReturn), fileExists)
[/b]
            if (fileExists)
            {
               recorder.ExpectAndReturn(HostingEnvironment.VirtualPathProvider.GetFile(resourceToReturn), mockVirtualFile).WhenArgumentsMatch(resourceToReturn);
            }


            recorder.ExpectAndReturn(mockVirtualFile.Open(), dataStream);

            
            if (dataStream != null)
            {
               recorder.ExpectAndReturn(HostingEnvironment.VirtualPathProvider.GetCacheDependency("", null, DateTime.Now), mockCacheDependency);
               mockHttpContext.Cache.Insert("", null, null);
            }

         }


If you try to mock it as shown then just call

HostingEnvironment.VirtualPathProvider.FileExists("foo");
HostingEnvironment.VirtualPathProvider.FileExists("foo");

the first will succeed and the second will throw the error.

Thanks.
Jeff
asked by JEberlin (1.7k points)

3 Answers

0 votes
Hi
This is a known issue.
The problem is that Repeat on chains create an ambiguity:
Did the user want to repeat the whole chain? Or he wants to repeat only the last call?

There are few work around solutions for this, you can see the details here
(Look at the tips section at the button).
We are working on a solution that will resolve the ambiguity problem.
answered by ohad (35.4k points)
0 votes
Your documentation states:

All RecordExpectations methods (Return, Throw, Unmocked) will handle only the LAST method of the chain.


It seems that should handle my case, unless I'm miunderstanding something. It's not a big deal as I can place another call in there for now, just wanting to fully understand.

Thanks!
Jeff
answered by JEberlin (1.7k points)
0 votes
Hi
Note that VirtualPathProvider class is an abstract class which make
it strict by default.
The call to the HostingEnvironment property VirtualPathProvider is not mocked
hence it returns unmocked VirtualPathProvider instance.
This 'breaks' the chain and leads to the Verify exception you get.

I agree the that the documentation is misleading in this context.
The behavior makes repeating on chains useless (but the work around will work).
We will fix this problem, thanks for pointing it out.
answered by ohad (35.4k points)
...