UnoSD / Moq.Dapper

Moq extensions for Dapper methods.
GNU General Public License v2.0
173 stars 78 forks source link

SetupDapperAsync() on Dapper ExecuteAsync() method throws NotSupportedException #31

Closed davemcmanus closed 5 years ago

davemcmanus commented 5 years ago

The following code causes a SystemNotSupportedException:

var mockDbConn = new Mock<IDbConnection>();
mockDbConn.SetupDapperAsync(x => x.ExecuteAsync(It.IsAny<string>(), 
    It.IsAny<object>(),
    It.IsAny<IDbTransaction>(),
    It.IsAny<int?>(),
    It.IsAny<CommandType?>())
).ReturnsAsync(1);

Not sure what is causing the issue. Any suggestions?

davemcmanus commented 5 years ago

I figured out the issue. I was using "IDbConnection" instead of "DbConnection". Perhaps the source should be updated to not throw exceptions on IDbConnection.

UnoSD commented 5 years ago

Hi @davemcmanus, the IDbConnection interface exposes IDbCommand which itself has no async methods at all (see https://docs.microsoft.com/en-us/dotnet/api/system.data.idbcommand?view=netframework-4.7.2 and https://docs.microsoft.com/en-us/dotnet/api/system.data.common.dbcommand?view=netframework-4.7.2).

The async versions are only present on the DbCommand class exposed by the DbConnection. Even when you use IDbConnection Dapper internally downcast it to a DbConnection therefore it would give you an exception anyway on that side.

davemcmanus commented 5 years ago

Hi @davemcmanus, the IDbConnection interface exposes IDbCommand which itself has no async methods at all (see https://docs.microsoft.com/en-us/dotnet/api/system.data.idbcommand?view=netframework-4.7.2 and https://docs.microsoft.com/en-us/dotnet/api/system.data.common.dbcommand?view=netframework-4.7.2).

The async versions are only present on the DbCommand class exposed by the DbConnection. Even when you use IDbConnection Dapper internally downcast it to a DbConnection therefore it would give you an exception anyway on that side.

@UnoSD Thanks for your reply. Makes sense.