ho un'interfaccia preesistente ...beffardo Metodi di estensione con Moq
public interface ISomeInterface
{
void SomeMethod();
}
e ho esteso questo intreface utilizzando un mixin ...
public static class SomeInterfaceExtensions
{
public static void AnotherMethod(this ISomeInterface someInterface)
{
// Implementation here
}
}
ho un i thats classe di chiamare questo che voglio provare ...
public class Caller
{
private readonly ISomeInterface someInterface;
public Caller(ISomeInterface someInterface)
{
this.someInterface = someInterface;
}
public void Main()
{
someInterface.AnotherMethod();
}
}
e un test in cui mi piacerebbe prendere in giro l'interfaccia e verificare la chiamata al metodo di estensione ...
[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();
}
eseguire questo test tuttavia genera un'eccezione ...
System.ArgumentException: Invalid setup on a non-member method:
x => x.AnotherMethod()
La mia domanda è: esiste un bel modo per deridere la chiamata mixin?
Nella mia esperienza, i termini mixin e metodi di estensione sono cose separate.Userò quest'ultimo in questo caso per evitare confusione: P –
Duplicato di: http://stackoverflow.com/questions/562129/how-do-i-use-moq-to-mock-an-extension-method. – Oliver