I tried to write test case for my DataAccess method containing QueryMultipleAsync but it being failed showing the error :
Error : Message: System.NotSupportedException : Unsupported expression: gr => gr.ReadAsync(It.IsAny()) Non-overridable members (here: SqlMapper.GridReader.ReadAsync) may not be used in setup / verification expressions.
so here is my DataAccess method where I have used QueryMultipleAsync. :
public async Task<bool> SummerData(Guid darkStoreId, Guid storeId, long id)
{
await _context.OpenConnectionForKeyAsync(darkStoreId);
var result= await _context.Connection.QueryMultipleAsync(_checkMappingStatus, new { storeId, id });
return await DataStocks(result);
}
so for this I have tried to write a test case :
[Fact]
public async Task SummerData()
{
var expectedResult = new List<object> { new { id = 1L, tenantClientId = tenantClientId } };
QueryMultiAsync(expectedResult);
sampleDataAccess.Setup(s => s.VerifySampleExistAsync(tenantId, tenantClientId, 1)).ReturnsAsync(false);
await data.VerifySampleExistAsync(tenantId, tenantClientId, 1);
string Name = "Test";
Assert.True(Name == classs.Name);
}
so there i have tried to mock the QueryMultiAsync
public object DataStocks(IEnumerable<dynamic> expected)
{
var mockGridReader = new Mock<GridReader>();
mockGridReader.Setup(a => a.ReadAsync(It.IsAny<Type>(), It.IsAny<bool>()))
.Returns<Type, bool>((type, buffered) => Task.FromResult(expected));
_iConnection.SetupDapperAsync(c => c.QueryMultipleAsync(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<IDbTransaction>(), It.IsAny<int?>(), It.IsAny<CommandType?>()))
.ReturnsAsync(mockGridReader.Object);
this._dbService.Setup(x => x.Connection).Returns(_iConnection.Object);
this._dbCatalogue.Setup(x => x.Connection).Returns(_connection.Object);
this._dbTransaction.Setup(x => x.Connection).Returns(_connection.Object);
return expected;
}
So this was the code. Now on running the test case it get stuck in this line :
mockGridReader.Setup(a => a.ReadAsync(It.IsAny<Type>(), It.IsAny<bool>()))
.Returns<Type, bool>((type, buffered) => Task.FromResult(expected));
Please If anyone knows how to fix this or write test case for this. Thank You!