Reputation: 681
I wonder if there is anyway one can fake up a generic method call, for all possible types (or specified sub-types)?
For example, suppose we have this wonderful IBar interface.
public interface IBar
{
int Foo<T>();
}
Can I fake a dependency to this IBar's Foo call, without having to specify T being any specific type?
[TestFixture]
public class BarTests
{
[Test]
public void BarFooDoesStuff()
{
var expected = 9999999;
var fakeBar = A.Fake<IBar>();
A.CallTo(() => fakeBar.Foo<T>()).Returns(expected);
var response = fakeBar.Foo<bool>();
Assert.AreEqual(expected, response);
}
}
Thanks!
Upvotes: 15
Views: 8266
Reputation: 241790
I'm not aware of any way to do this directly. I don't think DynamicProxy (which FakeItEasy uses) supports open generic types. However, there's a workaround, if you're interested.
There's a way to specify a call to any method or property on a fake. Check out the Where
and WithReturnType
bits in this passing test:
[TestFixture]
public class BarTests
{
[Test]
public void BarFooDoesStuff()
{
var expected = 9999999;
var fakeBar = A.Fake<IBar>();
A.CallTo(fakeBar)
.Where(call => call.Method.Name == "Foo")
.WithReturnType<int>()
.Returns(expected);
var response = fakeBar.Foo<bool>();
Assert.AreEqual(expected, response);
}
}
Still, though, I'm curious about the use for this. Do you have an example test that actually uses the faked interface as a dependency?
Upvotes: 25