❔ Test file not found case
Hello. How simply and cleanly to test file not found case? I have web api app with two services. One sorts int array and prints to text file and other loads last sorted result.
public SortingService(IConfiguration configuration)
{
_configuration = configuration;
}
public async Task<List<SortingDto>> SortingListAsync(int[] numberArray)
{
var sortingList = new List<SortingDto>();
sortingList.Add(BubbleSort(numberArray));
sortingList.Add(ArraySort(numberArray));
sortingList.Add(QuickSort(numberArray));
string sortedArray = string.Join(",", sortingList.First().SortedArray);
var savePath = SaveTextFile();
await File.WriteAllTextAsync(savePath, sortedArray);
return sortingList;
}
public async Task<string[]> LoadSortedArrayAsync()
{
var loadPath = SaveTextFile();
if (loadPath == null)
{
throw new FileNotFoundException();
}
string[] lines = await File.ReadAllLinesAsync(loadPath);
return lines;
}
private string SaveTextFile()
{
return _configuration.GetValue<string>("FileLocation:Path") ?? null;
}
public SortingService(IConfiguration configuration)
{
_configuration = configuration;
}
public async Task<List<SortingDto>> SortingListAsync(int[] numberArray)
{
var sortingList = new List<SortingDto>();
sortingList.Add(BubbleSort(numberArray));
sortingList.Add(ArraySort(numberArray));
sortingList.Add(QuickSort(numberArray));
string sortedArray = string.Join(",", sortingList.First().SortedArray);
var savePath = SaveTextFile();
await File.WriteAllTextAsync(savePath, sortedArray);
return sortingList;
}
public async Task<string[]> LoadSortedArrayAsync()
{
var loadPath = SaveTextFile();
if (loadPath == null)
{
throw new FileNotFoundException();
}
string[] lines = await File.ReadAllLinesAsync(loadPath);
return lines;
}
private string SaveTextFile()
{
return _configuration.GetValue<string>("FileLocation:Path") ?? null;
}
1 Reply
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.