Question

I have the following test:

IUnityContainer unityContainer = MockRepository.GenerateStrictMock<IUnityContainer>();

unityContainer.Expect(c => c.IsRegistered<IServiceContainerRegistrar>()).Return(true).Repeat.Once();

As far as I know I'm creating a mock of the IUnityContainer and I'm telling him what to return when someone calls the IsRegistered method.

I'm getting the following Exception:

Test method CommonInitializerTest.CommonInitializer_Initialize_WorksOnce threw exception: 
System.InvalidOperationException: Previous method 'IEnumerator.MoveNext();' requires a return value or an exception to throw.

With the following stacktrace:

at System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source)
at Microsoft.Practices.Unity.UnityContainerExtensions.IsRegistered(IUnityContainer container, Type typeToCheck, String nameToCheck)
at Microsoft.Practices.Unity.UnityContainerExtensions.IsRegistered(IUnityContainer container, Type typeToCheck)
at Microsoft.Practices.Unity.UnityContainerExtensions.IsRegistered(IUnityContainer container)
at Drives.Services.Common.Tests.CommonInitializerTest.<CommonInitializer_Initialize_WorksOnce>b__0(IUnityContainer c) in CommonInitializerTest.cs: line 50
at Rhino.Mocks.RhinoMocksExtensions.Expect(T mock, Function`2 action)

So the Expect is calling the real code and as I've not mocked everything used by the Unity it's failing. Why does RhinoMock execute real code when registering the expectation?????

Était-ce utile?

La solution

As far as I know there is no built-in way to Mock a static extension method. This it true with Moq, and I guess same for RhinoMock. Of course you can create wrappers etc, but I don't think there is built-in way. That's probably why your code hitting the real extension method even through it has been stubbed out.

public static bool IsRegistered<T>(this IUnityContainer container)
{
  Guard.ArgumentNotNull((object) container, "container");
  return UnityContainerExtensions.IsRegistered(container, typeof (T));
}

See also this relevant post.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top