jeudi 2 juillet 2015

Unable to mock base class' method in Moq even after CallBase = true/false

Here's my code:

public class Base {
   public Dependency Dependency {get;set;}
   public virtual Foo MethodA(Expression<Func<T,bool>> exp1, 
                              params Expression<Func<T, object>>[] exp2)
   {
      return Dependency.DoSomething(exp1);
   }
}

public class Derived : Base {
   public Foo DerviedMethod(string str) {
      return base.MethodA(e1 => e1.Prop.Equals(str), e2 => e2.Prop);
   }
}

And my Unit Test code:

var mock = new Mock<Derived> { CallBase = true }; // Same result with false
mock
   .Setup(m => m.MethodA(
      It.IsAny(Expression<Func<Bar, bool>>>(),
      It.IsAny(Expression<Func<Bar, object>>>()
      ))
   .Returns(new Bar());

// Act
var result = mock.Object.DerviedMethod("test");

// Assert
Assert.IsNotNull(result);

But it still calls the original method and not the mocked one. Both classes exist in same assembly.

I have searched about it and almost all people got it right with CallBase = true or false.

Any ideas what is wrong with above code?

Aucun commentaire:

Enregistrer un commentaire