用Moq模拟扩展方法
我有一个预先存在的界面......
public interface ISomeInterface{
void SomeMethod();}并且我使用mixin扩展了这个表面...
public static class SomeInterfaceExtensions{
public static void AnotherMethod(this ISomeInterface someInterface)
{
// Implementation here
}}我有一个叫这个我要测试的课程...
public class Caller{
private readonly ISomeInterface someInterface;
public Caller(ISomeInterface someInterface)
{
this.someInterface = someInterface;
}
public void Main()
{
someInterface.AnotherMethod();
}}和测试,我想模拟界面并验证对扩展方法的调用...
[Test]
public void Main_BasicCall_CallsAnotherMethod()
{
// Arrange
var someInterfaceMock = new Mock<ISomeInterface>();
someInterfaceMock.Setup(x => x.AnotherMethod()).Verifiable();
var caller = new Caller(someInterfaceMock.Object);
// Act
caller.Main();
// Assert
someInterfaceMock.Verify();
}然而,运行此测试会产生异常......
System.ArgumentException: Invalid setup on a non-member method:x => x.AnotherMethod()
我的问题是,有一个很好的方法来模拟混合调用吗?
阿晨1998
慕后森