chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi,

I'm not sure if there is a bug related to his post (I've seen some posts regarding generics issues...).

Here my example, hope to here from you soon.
Thanks

Production class:

public class MyItem
{
public Uri Id;
public string Data;
}

public class MyQuery
{
public string Query;
}

public interface IItemsProvider
{
IEnumerable<T> GetItems<T>(IEnumerable<Uri> ids, MyQuery query) where T : class;
}

public class BaseManager<T> where T : class
{

}

// When removing BaseManager<IItemsProvider> the test works
public class MyManager : BaseManager<IItemsProvider>, IItemsProvider
{
public IEnumerable<T> GetItems<T>(IEnumerable<Uri> ids, MyQuery query) where T : class
{
List<T> items = new List<T>();
return items;
}
}

public class RunTime
{
private MyManager _manager;

public RunTime()
{
_manager = new MyManager();
}

public MyManager Manager
{
get { return _manager; }
}
}

public class UsingTheManager
{
private RunTime _runtime;

public UsingTheManager(RunTime runtime)
{
_runtime = runtime;
}

public void SomeFunction(IEnumerable<Uri> ids)
{
_runtime.Manager.GetItems<MyItem>(ids, null);
}
}


Test class:

/// <summary>
///This is a test class for UsingTheManagerTest and is intended
///to contain all UsingTheManagerTest Unit Tests
///</summary>
[TestClass()]
public class UsingTheManagerTest
{

#region Additional test attributes

[TestInitialize()]
public void MyTestInitialize()
{
}

[TestCleanup()]
public void MyTestCleanup()
{
Isolate.CleanUp();
}

#endregion

/// <summary>
///A test for SomeFunction
///</summary>
[TestMethod()]
public void SomeFunctionTest()
{
RunTime runtimeMock = Isolate.Fake.Instance<RunTime>(Members.ReturnRecursiveFakes);
MyManager myManagerMock = Isolate.Fake.Instance<MyManager>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => runtimeMock.Manager).WillReturn(myManagerMock);

// The following throws:
// Test method TypeMockProblemsTests.UsingTheManagerTest.SomeFunctionTest threw exception:
// System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->
// System.InvalidOperationException: Operation is not valid due to the current state of the object..
Isolate.WhenCalled(() => myManagerMock.GetItems<MyItem>(null, null)).WillReturn(null);

IEnumerable<Uri> ids = new List<Uri>() { new Uri("bla://bla") };

UsingTheManager target = new UsingTheManager(runtimeMock);

target.SomeFunction(ids);
}
}
asked by mod (5.8k points)

1 Answer

0 votes
Currently we have a known issue when faking a method that returns a compound generic (IEnumerable<T>).

When we resolve this issue in the neat future I will let you know.
answered by dhelper (11.9k points)
...