I want to write a unit test for a method that returns data from a stored procedure, how do I mock this?
// Arrange
var mockDBContext = new Mock<MyDBContext>(options);
var mockData = await GetData();
mockDBContext.Setup(x => x.GetDataFromStoredProcedure(It.IsAny<int>(), It.IsAny<CancellationToken>())).ReturnsAsync(mockData);
Example of method that I want to test
public async Task<List<MyModel>> GetDataFromStoredProcedure(int Id, CancellationToken cancellationToken)
{
return await MyModel.FromSqlInterpolated($"EXEC {MyStoredProcedure} {Id}").ToListAsync(cancellationToken);
}
New contributor
user24640939 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5