Azure function based on .Net core 3.1. Triggered when a file is uploaded into the Blob container. Any files uploaded to Azure Blob Storage are compressed, and the database is updated.
Below is my business logic
public partial class FileProcessingServiceFacade : IFileProcessingServiceFacade
{
public async Task<string> ProcessAsync(Stream myBlob, string name, ILogger log, ExecutionContext context)
{
AppDbContext.FileRecords.Add(new FileRecords { ... });
AppDbContext.SaveChanges();
return await Task.FromResult($"success");
}
}
public partial class FileProcessingServiceFacade : ServiceBase<FileProcessingServiceFacade>
{
public FileProcessingServiceFacade(AppDbContext appDbContext, IOptions<AppConfigurator> configurator)
: base(appDbContext, configurator) { }
}
I am using xUnit and MOQ for unit testing and below is my unit test
[Fact]
public void ProcessAsync()
{
var filename = "fileName.csv";
var stream = new MemoryStream(File.ReadAllBytes(filename));
var log = Mock.Of<ILogger>();
var executionContext = Mock.Of<ExecutionContext>();
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase(databaseName: "Testing")
.Options;
var appDbContext = new AppDbContext(options);
var appSettings = new AppConfigurator(){ ... };
IOptions<AppConfigurator> configurator = Options.Create(appSettings);
var target = new FileProcessingServiceFacade(appDbContext, configurator);
var actual = target.ProcessAsync(stream, filename, log, executionContext);
Assert.True(appDbContext.FileRecords.FirstAsync<FileRecords>().Result.FileName.Equals(filename));
Assert.True(actual.Result.Equals("success"));
stream.Dispose();
}
I am trying to improve the code quality of the unit testing and I would appreciate any suggestions.
The Application test involves one or more external systems, below are the sample points which you need to be aware while have unit testing
Also, As suggested by BrettMiller you can go through the sample of Azure functions Test fixture.