I'm having trouble mocking a generic method on a static class. The class and method I'm trying to mock look as such:
public static class AgentFactory
{
private static Dictionary<string, IAgent> _agentDictionary;
static AgentFactory()
{
_agentDictionary = new Dictionary<string, IAgent>();
}
public static T GetAgent<T>() where T : IAgent
{
if (!_agentDictionary.ContainsKey(typeof(T).Name))
{
_agentDictionary.Add(typeof(T).Name, LoadAgent<T>());
}
return (T)_agentDictionary[typeof(T).Name];
}
}
I'm trying to mock the GetAgent<T> method so that it returns an instance of another mock I've created:
[TestFixture]
public class AccountAgentTests
{
private Mock _agentFactoryMock;
private MockObject _accountAgentMock;
[SetUp]
public void Setup()
{
_agentFactoryMock = MockManager.Mock(typeof(AgentFactory));
_accountAgentMock = MockManager.MockObject(typeof(IAccountAgent));
_agentFactoryMock.ExpectAndReturn("GetAgent", _accountAgentMock.MockedInstance, typeof(IAccountAgent));
[... set up other mocks here ...]
}
}
When I run this though, TypeMock throws an exception, complaining about the return type of the generic method call:
------ Test started: Assembly: MyCompany.AccountAgent.UnitTests.dll ------
TestCase 'MyCompany.AccountAgent.UnitTests.AccountAgentTests.MyUnitTest'
failed: TypeMock.TypeMockException :
*** Method GetAgent in type MyCompany.Agent.Factory.AgentFactory returns and not TypeMock.Mock+a.
at w.b(Type A_0, String A_1, Type A_2, Type[] A_3)
at TypeMock.Mock.a(String A_0, Object A_1, Boolean A_2, Boolean A_3, Int32 A_4, Type[] A_5)
at TypeMock.Mock.ExpectAndReturn(String method, Object ret, Int32 timesToRun, Type[] genericTypes)
at TypeMock.Mock.ExpectAndReturn(String method, Object ret, Type[] genericTypes)
MyCompanyAccountAgentUnitTestsAccountAgentTests.cs(38,0): at MyCompany.AccountAgent.UnitTests.AccountAgentTests.Setup()
Line 38 in the error message corresponds to the 'ExpectAndReturn' line above.
I'm using TypeMock Isolator 4.2.4; I recently upgraded from 4.1.0 (the above code worked fine in 4.1.0, but I had other problems with mocking 'ref' parameters).
Any help appreciated -
Thanks,
Nick