我有一个预先存在的接口嘲讽扩展方法...与起订量
public interface ISomeInterface
{
void SomeMethod();
}
,我已经使用一个混合扩展这个intreface ...
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()
我的问题是,有没有嘲笑了混入通话的好办法?
根据我的经验,术语mixin和扩展方法是分开的东西。在这种情况下,我会使用后者来避免混淆:P – 2010-02-19 12:33:49
重复:http://stackoverflow.com/questions/562129/how-do-i-use-moq-to-mock-an-extension-method。 – Oliver 2012-10-12 13:05:24