0

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?????

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • How you assign the stubbed unity container to the system under test. Would you be able provide more code in you unit test obviously hide any thing that you not suppose to post ? Also just check whether isRegistered is really mockable i.e might be an static ext method which aren't mockable. – Spock Nov 12 '13 at 08:12
  • You need you post your extension method. It looks like some code within is using a linq expression against your mocked container. – TheCodeKing Nov 12 '13 at 08:26
  • @Raj: maybe you wrote the solution. As it is an extension method maybe it cannot be mocked right? – Ignacio Soler Garcia Nov 12 '13 at 08:42
  • @TheCodeKing: I know but as I'm registering an expectation I was surprised to find out that the real extension method was called. – Ignacio Soler Garcia Nov 12 '13 at 08:43
  • You are but it looks like there is some other code you've not posted that is using linq. This requires additional expectations as you are using a strict mock. It's not real code that's running, it's caused by additional calls to the interface that aren't mocked. – TheCodeKing Nov 12 '13 at 08:46
  • @TheCodeKing: the two lines available are the whole Unit Test code. The Linq failing is inside the REAL IsRegistered method of the UnityContainer (which is mocked). – Ignacio Soler Garcia Nov 12 '13 at 08:48
  • @SoMoS yes I think so - as per my answer below. – Spock Nov 12 '13 at 09:39

1 Answers1

1

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.

Community
  • 1
  • 1
Spock
  • 7,009
  • 1
  • 41
  • 60