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