4
我有一些通用方法的接口。這些方法根據傳入的數據類型執行操作。我如何用NSubstitute模擬此操作?目前,我不得不求助於使用具體類而不是模擬,因爲我無法處理該方法將被調用的所有可能的類型。使用NSubstitute嘲弄通用方法
public interface IInstanceSource
{
bool CanCreate<T>();
T Create<T>();
void Register<T>(Func<T> creator);
}
public static IInstanceSource GetInstanceSource()
{
var _data = new Dictionary<Type, Func<object>>();
var a = Substitute.For<IInstanceSource>();
//code below fails since T is not defined. How do I make the code below accept any type?
a.WhenForAnyArgs(x=>x.Register(Arg.Any<Func<T>>)).Do(x=> { /* todo */});
a.CanCreate<T>().Returns(x => _data[typeof (T)]);
return a;
}
謝謝。
謝謝。我必須編寫一個實現''''IInstanceSource''''的類。它工作得很好。我雖然也許它是默認支持,我沒有看到它。 – ritcoder