I have an interface like this:
public interface IConfigManager {
T GetSetting<T>();
}
On test project, I add fake for above dll, but cannot write fake method for it. Open the generated code, it looks like:
public class StubIConfigManager : StubBase<IConfigManager>, IConfigManager
{
// Summary:
// Sets stubs of GetSetting()
public void GetSettingOf1<T>(FakesDelegates.Func<T> stub);
}
Because GetSettingOf1
was not define as a delegate so I can use lambda expression to fake.
How can I fake it?
Because the method is generic, a single delegate cannot suffice. One delegate cannot be both Func<string>
and Func<int>
. The method you're seeing allows you to pass in the delegate for a specific type, which is probably stored in a dictionary internally (from my attempts to replicate Fakes' stub behavior).
So, just pass in the delegate you'd normally assign to the property into the GetSettingOf1
method. This is really just a way to allow a generic method to have a stub implementation for any number of types rather than just one.
Example:
var configManager = new StubIConfigManager();
configManager.GetSettingOf1(() => "TestString");
configManager.GetSettingOf1(() => 23);
var stringResult = configManager.GetSetting<string>();
var intResult = configManager.GetSetting<int>();
Assert.AreEqual("TestString", stringResult);
Assert.AreEqual(23, intResult);
Clearly this example is not a test you should write, because Fakes works, but it should get the point across.