I’m trying to unit test the following method
public List<string> GetFilesInGCS(string bucket, string node)
{
List<string> fileNames = new List<string>();
try
{
log.Info($"Getting files from {bucket}...");
foreach (var storageObject in storageClient.ListObjects(bucket, node))
fileNames.Add(storageObject.Name);
log.Info($"Total files: {fileNames.Count}");
}
catch (Exception ex)
{
Task.Run(async () => { await _emailer.SendErrorAsync(ex); });
log.Error(ex);
}
return fileNames;
}
The problem is I don’t know how to properly mock the return for the ListObjects methods. It returns an abstract class of PageEnumerable<Objects, Object>
This is what I have so far.
[Test]
public void TestGetFilesInGCS()
{
Mock<StorageClient> storageClientMock = new Mock<StorageClient>(MockBehavior.Loose);
storageClientMock.Setup(m => m.ListObjects(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ListObjectsOptions>()));
GoogleService g = new GoogleService(storageClientMock.Object);
var files = g.GetFilesInGCS("test", "test");
Assert.That(files, Is.Not.Empty);
}