Home > other >  Mock method return value inside a method
Mock method return value inside a method

Time:12-09

So I have method that validates user

public Boolean ValidateUser(username){

    return ValidationHelper(username)

}

How do I mock the return value of ValidationHelper? I tried something like following

Helper.Setup(item => item.ValidateUser(It.IsAny<String>())).Returns(true);

But I got the error:

System.NotSupportedException : Unsupported expression: x=> x.ValidateUser(It.IsAny<string>())
Non-overridable members (here: UserValidation.ValidateUser) may not be used in setup / verification expressions.

CodePudding user response:

You are not mocking ValidationHelper, but the ValidateUser method. You can only mock interfaces or virtual members. In your case, you can change the definition to

public virtual Boolean ValidateUser(username) {...}

I don't know whether that's the best option in your case though, because you don't show the call site or the test.

  • Related