You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Oct 21, 2018. It is now read-only.
Hi*
I have the following issue when mocking interface that has a method that returns Func delegate.
Please see the following code:
Dummy class model just to show the issue
public interface ITest<T>
{
Func<T> Get(string text);
}
public interface IParam
{
string Text { get; }
}
public class Param : IParam
{
public string Text { get; set; }
}
Mocking code looks like this
var mock = MockRepository.GenerateMock<ITest<IParam>>();
mock.Expect(m => m.Get("Test1")).Return( () => new Param { Text = "ParamWithText1" }); //OK
mock.Expect(m => m.Get("Test2")).Return( () => new Param { Text = "ParamWithText2" }); //Exception
The issue is that the fist expect runs without any problems (suppose that I commented second one) but with the second it throws InvalidCastException exception.
Message
Unable to cast object of type
'Castle.Proxies.ProxyDelegate_Func`1_1Proxye77fc793043240708536f884e313f671' to type 'System.Func`1[IParam]'.
I tried different combination and I suspect that there are some issue with delegate but strange thing is that the first time is passes.
I would be grateful for any ideas if I'm missing something or how to solve it.
Thanks,
Norbert Raus
The text was updated successfully, but these errors were encountered:
Hello,
I couldn't wait so long for response so I analized why rhino was confused with second expectation. It turned out that in RecordMockState class there is a method called TryCreateReturnValue which evaluates return object from a expectation. There is a foreach statement with iterates through DependedMocks where after first expectation first mock is added. Then it tries to use already created results that was alread created if the following condition is met:
type == expectation.Method.ReturnType
where type is item from dependentMock.ImplementedTypes.
I have checked my situation it passes this condition but later on there is this code.
returnValue = dependentMock.MockedObjectInstance;
For my situation the above condition is not enough after adding:
expectation.Method.ReturnType.IsAssignableFrom(dependentMock.MockedObjectInstance.GetType())
started working.... I run all rhino unit tests to see if this breaks and one test if failing :
FieldProblem_LAFAY.
Can someone verify if this is bug or I'm missing something. For now is seems that it works for me (I'm not using marshaled objects -> failing test).
Hi*
I have the following issue when mocking interface that has a method that returns Func delegate.
Please see the following code:
Dummy class model just to show the issue
Mocking code looks like this
The issue is that the fist expect runs without any problems (suppose that I commented second one) but with the second it throws InvalidCastException exception.
Message
I tried different combination and I suspect that there are some issue with delegate but strange thing is that the first time is passes.
I would be grateful for any ideas if I'm missing something or how to solve it.
Thanks,
Norbert Raus
The text was updated successfully, but these errors were encountered: