UnoSD / Moq.Dapper

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

ExecuteScalarAsync does not work with specific type in generic parameter. #57

Closed honzakuzel1989 closed 3 years ago

honzakuzel1989 commented 4 years ago

Hi, the example with object in ExecuteScalarAsync generic works well (xUnit version)

[Fact()]
public async Task ExecuteScalarAsyncTest()
{
    var connection = new Mock<DbConnection>();
    const string expected = "Hello";
    connection.SetupDapperAsync(c => c.ExecuteScalarAsync<object>(It.IsAny<string>(), null, null, null, null))
              .ReturnsAsync(expected);
    var actual = await connection.Object.ExecuteScalarAsync<object>("");
    Assert.Equal(actual, expected);
}

but when I change object to desired specific type, e.g. string the test throws exception in SetupDapperAsyncMethod

[Fact()]
public async Task ExecuteScalarAsyncTest()
{
    var connection = new Mock<DbConnection>();
    const string expected = "Hello";
    connection.SetupDapperAsync(c => c.ExecuteScalarAsync<string>(It.IsAny<string>(), null, null, null, null))
              .ReturnsAsync(expected);
    var actual = await connection.Object.ExecuteScalarAsync<string>("");
    Assert.Equal(actual, expected);
}

with followed message

  Message: 
    System.InvalidCastException : Unable to cast object of type 'Castle.Proxies.ISetup`2Proxy' to type 'Moq.Language.Flow.ISetup`2[System.Data.Common.DbConnection,System.Threading.Tasks.Task`1[System.String]]'.
  Stack Trace: 
    DbConnectionMockExtensions.SetupDapperAsync[TResult](Mock`1 mock, Expression`1 expression)
honzakuzel1989 commented 4 years ago

PR #59 and #60.

UnoSD commented 3 years ago

thanks for the PR, merged