huysentruitw / entity-framework-core-mock

Easy Mock wrapper for mocking EFCore5 DbContext and DbSet using Moq or NSubstitute

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mock _dbContext.Database, ExecuteSqlCommandAsync in EFCore

cbjpdev opened this issue · comments

Hi,

I want to mock ExecuteSqlCommandAsync and ExecuteReader. We have to use these due to specific reason. This may not be a feature request. Please explain me how to achieve mocking these?

case 1:
using (var command = _dbContext.Database.GetDbConnection().CreateCommand())
{
command.CommandText = string.Format("select NEXT VALUE FOR {0}", sequenceName);
_dbContext.Database.OpenConnection();
using (var result = command.ExecuteReader())
{
if (result.Read())
{
seqValue = result[0].ToString();
}
}
}

case 2:
createdCount = await _reapContext.Database.ExecuteSqlCommandAsync(createSql);

I think you need to use an integration test to cover that. There's no use in mocking a reader like that in my opinion.

Yes, that would be the option. Anyway I added few interfaces and mocking these. No blocker for the moment. Can test these in the integration testing.

Ok I'll close it for now. If you have any specific request regarding this which could benifit the library, feel free to reopen.