Search code examples
unit-testingmoqprotected

Ignoring argument of a List type for a protected method in Moq


I'm trying to setup an expected call to a protected method. The methods signature looks like this:

protected SqlDataReader MethodName(string Name, List<SqlParameter> paramList, SqlConnection con)

I've come as far as setting up the expectation as follows, but I get an error when running the test:

mock.Protected()
    .Setup<SqlDataReader>( "MethodName", "SomeString", ItExpr.IsAny<List<SqlParameter>>(), ItExpr.IsNull<SqlConnection>() )
    .Returns( dataReader );

The error I get is:

Test method GlobalTests.DBAdapterSystemDataTest.GetDentalWingsProstheticTypeMappings threw exception: 
System.NotSupportedException: Invalid setup on a non-virtual (overridable in VB) member: mock => mock.ExecuteReaderStoredProcedure("GetDentalWingsProstheticTypeMappings", It.IsAny<List`1>(), It.Is<SqlConnection>(v => Object.Equals((Object)v, (Object)null)))

Does anyone have an idea how I can set up the expectation so that it works? I don't care for any argument values, I just want to chech that the method was called at least once.


Solution

  • The error message implies that you need to add the virtual keyword to your MethodName method, so that Moq can override it in the tests.