Question

I am having trouble setting up the configuration of Spring.Net so that I can use Rhino Mocks to generate a mock object. I realise that GenerateMock is a static method and so I need to use the factory-method in the config, but I just can't get it to work. This is the configuration I am using:

<object id="exampleObject"
        type="Rhino.Mocks.MockRepository, Rhino.Mocks"
factory-method="GenerateMock&amp;lt;MyAssembly.MyInterface>" />

Then in my code (which is a unit test) I use:

using (IApplicationContext ctx = ContextRegistry.GetContext()) {....}

but I get the following error message:

System.Configuration.ConfigurationErrorsException: Error creating context 'spring.root': Could not load type from string value 'MyAssembly.MyInterface'. --->  System.TypeLoadException: Could not load type from string value 'MyAssembly.MyInterface'..

Any ideas why I might be getting the error?

Was it helpful?

Solution

Actually you only specify the typename but not the assembly name in the generics argument. It should better read

  factory-method="GenerateMock<[MyNamespace.MyInterface, MyAssembly]>"

Note the square brackets to quote the full qualified typename and you need to grab a recent nightly build.

The full qualified name allows the CLR to locate the assembly containing your type. Otherwise Spring scans already loaded assemblies for that typename. If your assembly wasn't loaded yet, you experience the errormessage you face.

OTHER TIPS

Not sure which version of rhino mocks you were using but this doesn't seem to work anymore because Spring.NET doesn't correctly interpret the variable nr of arguments in GenerateMock(params ...).

It took me half a day to figure this out and use the following workaround:

<object id="Logger" type="Rhino.Mocks.MockRepository, Rhino.Mocks"
            factory-method="GenerateMock&lt;Core.Common.Logging.ILog>" >
      <constructor-arg name="argumentsForConstructor">
            <list element-type="System.Object">             
            </list>
        </constructor-arg>
</object>

I basically had to explicitly pass an empty array of objects so that Spring would recognize the method signature...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top