I'm writing an API in ASP.NET Core 3.1, using EF Core to access a SQL Server database. I have a function in the API that needs to call a stored procedure with several input parameters and an output parameter. A simplified version of this function is below.
I am using a DbContext with .UseInMemoryDatabase() for other tests, but the in memory database cannot be used with stored procedures.
(This solution is database first, not code first. It would be possible to change the stored procedure if necessary but it would be much better if I didn't have to. I could change my C# function to call the stored procedure a different way though if that helps.)
How do I unit test this function?
public class MyFoo : IFoo
{
public ApplicationDbContext DbContext { get; }
public MyFoo(ApplicationDbContext dbContext)
{
DbContext = dbContext;
}
public async Task<bool> GetMyStoredProcResult(string val1, string val2, string val3, string val4, string val5)
{
// input validation removed for brevity
var p1 = new SqlParameter
{
ParameterName = "p1",
DbType = System.Data.DbType.String,
Direction = System.Data.ParameterDirection.Input,
Value = val1
};
// p2 - p5 removed for brevity
var resultParam = new SqlParameter
{
ParameterName = "Result",
DbType = System.Data.DbType.Boolean,
Direction = System.Data.ParameterDirection.Output
};
var sql = "EXEC sp_MyProcedure @p1, @p2, @p3, @p4, @p5, @Result OUTPUT";
_ = await DbContext.Database.ExecuteSqlRawAsync(sql, p1, p2, p3, p4, p5, resultParam);
return (bool)resultParam.Value;
}
}