I followed this question Using Moq to mock only some methods but still threw out an exception.
Here is my main class. Foo
acts as a decorator.
public class Foo {
public Bar _b {get; set;}
public Foo(Bar b) {
this._b = b;
}
public bool MyMethod(){
return ComplexMethod(_b.name);
}
public bool ComplexMethod(){
...
}
}
Test class
[TestClass]
public class Foo {
[TestMethod]
public void TestFoo() {
var b = new Bar() {name = "name"};
var mock = new Mock<Foo>(b);
mock.CallBase = true;
mock.Setup(x => x.ComplexMethod()).Returns(true);
var result = mock.Object.MyMethod();
...
}
}
Threw out exception:
System.NotSupportedException: 'Unsupported expression: x => x.ComplexMethod() Non-overridable members (here: Foo.ComplexMethod) may not be used in setup / verification expressions.'