[TestFixture]
public class TestWithMethodHavingAGenericReturnType
{
[Test]
public void Execute ()
{
MockRepository mocks = new MockRepository ();
IGenericType<object> mock =
mocks.CreateMock<IGenericType<object>> ();
IMethodOptions methodOptions = Expect.Call (mock.MyMethod ());
//Assert.IsNotNull (methodOption.expectation.Method.ReturnType,
"True in 2.9.6");
methodOptions.Do ((MyDelegate) delegate { return new object
(); });
}
}
public interface IGenericType<T>
{
T MyMethod ();
}
public delegate object MyDelegate ();
(1) In the Orcas CTP the mscorlib gets updated to 2.0.50727.1302. The
behavior is different then with the ReturnType actually having a
value.
Regards, Michael
[...]
To add some more information to this issue - the line
"methodOptions.Do(...);" fails with the following exception:
System.InvalidOperationException: The delegate return value should be
assignable from
at
Rhino.Mocks.Expectations.AbstractExpectation.AssertReturnTypeMatch(Delegate
value) in C:\...\RhinoMocks\Sources\rhino-mocks\Rhino.Mocks
\Expectations\AbstractExpectation.cs:line 483
at
Rhino.Mocks.Expectations.AbstractExpectation.set_ActionToExecute(Delegate
value) in C:\...\RhinoMocks\Sources\rhino-mocks\Rhino.Mocks
\Expectations\AbstractExpectation.cs:line 202
at Rhino.Mocks.Impl.MethodOptions.Do(Delegate action) in C:\...
\RhinoMocks\Sources\rhino-mocks\Rhino.Mocks\Impl\MethodOptions.cs:line
87
at Tests.Test.Execute() in C:\...\GenericRhinoTests
\GenericRhinoTests\Test.cs:line 23
Even with mscorlib 2.0.50727.1302, this doesn't work.
The reason for this is that RhinoInvocation stores the method obtained
via DynamicProxy's IInvocation.Method property in its repository.
IInvocation.Method points to the original, uninstantiated
IGenericType<>.MyMethod, which has a generic return type (T). This
generic return type is not assignable from the return type of the
method passed to Do (object), leading to the exception.
To remedy this, RhinoInvocation should store the instantiated
IGenericType<object>.MyMethod (or its subclasses) in its repository.
I'm not sure whether the problem should be fixed in RhinoMocks or
DynamicProxy 2, though. I guess DP 2 shouldn't return
IGenericType<>.MyMethod in its IInvocation.Method property - it was
asked to proxy IGenericType<object> after all (unless I'm missing
something).
Regards,
Fabian