I'm currently unit testing my code and I have problem with creating a mock using Moq. I'm unit testing Foo, and I wish to mock User.GetSomething() to isolate the test. I have created a qick sample of the code that I wish to test:
public class User(){
public List<int> GetSomething(){
return bar.CreateList();
}
}
public class Foo(User user){
public int SomeMethod() {
var x = User.GetSomething()
Do something with x
...
}
}
I have tried creating a mock by doing the following. I have excluded the assert and unnecessary arrange for this question:
var mock = new Mock<User>();
List<int> someList = new List<int>(){1, 2, 3};
mock.Setup(u => u.CreateList()).Returns(SomeList);
Foo(mock.object)
var res = Foo.SomeMethod();
etc.
But I get the exception: System.NotSupportedException : Unsupported expression: u => u.CreateList()
Non-overridable members (here: User.CreateList) may not be used in setup / verification expressions.