How does one setup a generic method using moq library in C#? Such as
Interface IA
{
void foo();
void Get<T>();
}
[Fact]
public void SetupGenericMethod()
{
var mock = new Mock<IA>();
mock.Setup(x=> x.Get<It.IsAny<???>()>()
}
If you don't need to do something that related to the type T
, It can be done since Moq 4.13 (2019-09-01) by using It.IsAnyType
for generic type arguments:
mock.Setup(x => x.Get<It.IsAnyType>())
Full Example:
public interface IA
{
void Get<T>();
}
[Fact]
public void test()
{
// Arrange
bool didCallBackCalled = false;
var mock = new Mock<IA>();
mock.Setup(x => x.Get<It.IsAnyType>()).Callback(() => didCallBackCalled = true);
// Act
mock.Object.Get<string>();
// Assert
Assert.IsTrue(didCallBackCalled);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With