我正在编写一个小型库,该库使用Google Drive API v3 Client Library for C#。 对我而言,它涉及很多痛苦,因为我不能为此目的使用TDD风格。
执行该作业的常规方法如下所示:
internal async Task<Google.Apis.Drive.v3.Data.FileList> ReadFileList(string parentId, string pageToken, int pageSize)
{
// Define parameters of request.
var listRequest = this.driveService.Files.List();
listRequest.PageSize = pageSize;
listRequest.Q = "mimeType='application/vnd.google-apps.folder' and ('" + parentId + "' in parents)";
listRequest.Fields = "nextPageToken, files(id, name)";
listRequest.PageToken = pageToken;
// List files.
return await listRequest.ExecuteAsync();
}
对于单元测试,它包含一些依赖项(可能使用工厂方法模式重构):
对于集成测试,还有更多问题:
是否有类似&#34; In-Memory&#34;实体框架核心提供商? 如何才能使Google Drives Unit和Integration Tests易于管理?
答案 0 :(得分:2)
因此,对于Enterprise,您很幸运。
假设一个样本项目如下:
Google.Apis.Drive.v3
这是实际的程序
public class Class1
{
public async Task<Google.Apis.Drive.v3.Data.FileList> ReadFileList(string parentId, string pageToken, int pageSize)
{
// get the service somehow.
var ds = new DriveService();
var listRequest = ds.Files.List();
listRequest.PageSize = pageSize;
listRequest.Q = "mimeType='application/vnd.google-apps.folder' and ('" + parentId + "' in parents)";
listRequest.Fields = "nextPageToken, files(id, name)";
listRequest.PageToken = pageToken;
// List files.
return await listRequest.ExecuteAsync();
}
}
我们可以利用Microsoft Fakes框架的强大功能对这里的所有内容进行单元测试,而无需连接到互联网,谷歌驱动器,帐户等。
为您的类所在的库生成fakes程序集(在本例中为Google.Apis.Drive.v3
和Google.Apis
,您正在使用两者中的类型):
编写单元测试:
喜欢这样
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create())
{
int? usedPageSize = 0;
var usedQ = string.Empty;
var usedFields = string.Empty;
var usedPageToken = string.Empty;
Google.Apis.Drive.v3.Fakes.ShimFilesResource.ShimListRequest.AllInstances.PageSizeSetNullableOfInt32 =
(request, i) => usedPageSize = i;
Google.Apis.Drive.v3.Fakes.ShimFilesResource.ShimListRequest.AllInstances.QSetString = (request, s) => usedQ = s;
Google.Apis.Drive.v3.Fakes.ShimDriveBaseServiceRequest<Google.Apis.Drive.v3.Data.FileList>.AllInstances
.FieldsSetString = (request, s) => usedFields = s;
Google.Apis.Drive.v3.Fakes.ShimFilesResource.ShimListRequest.AllInstances.PageTokenSetString = (request, s) => usedPageToken = s;
Google.Apis.Requests.Fakes.ShimClientServiceRequest<Google.Apis.Drive.v3.Data.FileList>.AllInstances
.ExecuteAsync =
request =>
Task.FromResult(
new FileList
{
ETag = "hello",
Files = new List<File> { new File { Name = "imafile" } },
IncompleteSearch = false,
Kind = "Somekind",
NextPageToken = null
});
Google.Apis.Drive.v3.Fakes.ShimFilesResource.AllInstances.List = resource => (FilesResource.ListRequest)FormatterServices.GetUninitializedObject(typeof(FilesResource.ListRequest));
Google.Apis.Drive.v3.Fakes.ShimDriveService.Constructor = service => { }; // do not init the class
Google.Apis.Drive.v3.Fakes.ShimDriveService.AllInstances.FilesGet = service => (FilesResource)FormatterServices.GetUninitializedObject(typeof(FilesResource));
var target = new Class1();
var result = target.ReadFileList("parent", "token", 42).Result;
Assert.AreEqual(42, usedPageSize);
Assert.AreEqual("mimeType='application/vnd.google-apps.folder' and ('parent' in parents)", usedQ);
Assert.AreEqual("nextPageToken, files(id, name)", usedFields);
Assert.AreEqual("token", usedPageToken);
Assert.AreEqual(1, result.Files.Count);
Assert.AreEqual("imafile", result.Files[0].Name);
Assert.AreEqual("hello", result.ETag);
Assert.IsFalse(result.IncompleteSearch.Value);
Assert.AreEqual("Somekind", result.Kind);
Assert.IsNull(result.NextPageToken);
}
}
}