I am trying to mock the Database.ExecuteSqlCommand but it prompted me Unsupported expression Is there anyway that I can mock the repository to mock dBContext.Database.ExecuteSqlCommand
Repository.cs
private readonly Mock<DBContext> _dbContextMock;
public ShippingBulkRepositoryTest()
{
_dbContextMock = new Mock<DBContext>(new DbContextOptionsBuilder<DBContext>().Options);
_repository = new Repository(_dbContextMock.Object);
}
public async Task deleteAllRecords(CancellationToken cancellationToken)
{
try
{
dBContext.Database.ExecuteSqlCommand("DELETE FROM bulk");
dBContext.SaveChanges();
} catch (Exception ex)
{
Console.WriteLine(ex);
}
}
RepositoryTest
[Fact]
public async Task deleteAllRecords_should_call_GetAllSortedByPlateAsync_onto_service()
{
//given
var bulk = _fixture.Build<bulk>()
.With(x => x.id, 1)
.With(x => x.type, "bulk")
.CreateMany(1);
//_dbContextMock.Setup(x => x.Database.ExecuteSqlRaw(It.IsAny<string>()));
//when
await _repository.deleteAllRecords(default);
//then
_dbContextMock.Verify(x => x.SaveChanges(), Times.Once);
}
Error
Message:
Value cannot be null. (Parameter 'databaseFacade')