Tôi có một giao diện từ trước ...Mocking Phương pháp mở rộng với Moq
public interface ISomeInterface
{
void SomeMethod();
}
và tôi đã mở rộng intreface này sử dụng một mixin ...
public static class SomeInterfaceExtensions
{
public static void AnotherMethod(this ISomeInterface someInterface)
{
// Implementation here
}
}
Tôi có một thats lớp gọi này mà tôi muốn thử nghiệm ...
public class Caller
{
private readonly ISomeInterface someInterface;
public Caller(ISomeInterface someInterface)
{
this.someInterface = someInterface;
}
public void Main()
{
someInterface.AnotherMethod();
}
}
và thử nghiệm nơi tôi muốn giả lập giao diện và xác minh cuộc gọi đến phương pháp mở rộng ...
[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();
}
Chạy tuy nhiên thử nghiệm này tạo ra một ngoại lệ ...
System.ArgumentException: Invalid setup on a non-member method:
x => x.AnotherMethod()
Câu hỏi của tôi là, là có một cách tốt đẹp để thử ra các cuộc gọi mixin?
Theo kinh nghiệm của tôi, các thuật ngữ mixin và các phương pháp mở rộng là những điều riêng biệt.Tôi muốn sử dụng sau này trong trường hợp này để tránh hỗn hợp: P –
Bản sao của: http://stackoverflow.com/questions/562129/how-do-i-use-moq-to-mock-an-extension-method. – Oliver