-1
    string procedure ="updateRecords.sql"
    DynamicParameters parameters = new DynamicParameters();

    parameters.Add("@name", "rohan");
    parameters.Add("@age", 23);
    parameters.Add("@returnValue", DbType.Int32, direction: ParameterDirection.ReturnValue);

    repository.Update(procedure, parameters);
    var result = parameters.Get<int>("@returnValue"); 

when i try to mock

   var GetReturn = "3";
   var mockGet = new Mock<DynamicParameters>();

   mockGet.Setup(x=> x.Get<string>(GetReturn)).Returns(GetReturn); 

I got this exception,

System.NotSupportedException: 'Unsupported expression: x => x.Get("@returnValue") Non-overridable members (here: DynamicParameters.Get) may not be used in setup / verification expressions.'

please suggest way to solve this exception

  • Does this answer your question? [Moq - Non-overridable members may not be used in setup / verification expressions](https://stackoverflow.com/questions/56905578/moq-non-overridable-members-may-not-be-used-in-setup-verification-expression) – devNull Nov 18 '20 at 15:56

1 Answers1

1

Mock can only mock, either Interface methods, abstract methods or virtual methods.

The Get function does not fall under any of these categories.

So, if you can't change the DynamicParameters class, then you'll have to create a wrapper around it mock that wrapper.

In General, we don't unit test the DB access. We mock the classes that implement the DB access, so that we can test the business functionality.

It's not worth it to create the wrapper for the Dapper class and replace all your code just to unit test pretty much no behavior.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61