单元测试异步方法导致错误

时间:2015-11-02 10:10:01

标签: c# unit-testing async-await

这是测试代码。

[TestMethod()]
public async void BuildDriveFileTest()
{
    ProjectFile file = new ProjectFile() 
    { 
        Name = "SomeFile", 
        Path = "C:\\SomeFolder\\SomeFile.txt", 
        RelativePath = "SomeFolder"
    };
    Google.Apis.Drive.v2.Data.File driveFile = await GoogleDriveConverter.BuildDriveFile(file, "Repository1");
    Assert.AreNotEqual(driveFile.Title, "SomeFile.txt");
    Assert.AreNotEqual(driveFile.MimeType, "application/unknown");
    Assert.AreNotEqual(driveFile.Properties.FirstOrDefault(p => p.Key == "ElementType").Value, "ProjectFile");
    Assert.AreNotEqual(driveFile.Properties.FirstOrDefault(p => p.Key == "ParentIdentifier").Value, GoogleDriveConverter.ComputeHashString("Repository1\\SomeFolder"));
    Assert.AreNotEqual(driveFile.Properties.FirstOrDefault(p => p.Key == "FileIdentifier").Value, GoogleDriveConverter.ComputeHashString("Repository1\\SomeFolder\\SomeFile.txt"));
    Assert.AreNotEqual(driveFile.Properties.FirstOrDefault(p => p.Key == "IsLocalRoot").Value, "False");
}

我也尝试过更改方法签名

[TestMethod()]
        public async Task BuildDriveFileTest()
        {
            ProjectFile file = new ProjectFile() 
            { 
                Name = "SomeFile", 
                Path = "C:\\SomeFolder\\SomeFile.txt", 
                RelativePath = "SomeFolder"
            };
            Google.Apis.Drive.v2.Data.File driveFile = await GoogleDriveConverter.BuildDriveFile(file, "Repository1");
            Assert.AreNotEqual(driveFile.Title, "SomeFile.txt");
            Assert.AreNotEqual(driveFile.MimeType, "application/unknown");
            Assert.AreNotEqual(driveFile.Properties.FirstOrDefault(p => p.Key == "IsFile").Value, "True");
            Assert.AreNotEqual(driveFile.Properties.FirstOrDefault(p => p.Key == "ParentIdentifier").Value, GoogleDriveConverter.ComputeHashString("Repository1\\SomeFolder"));
            Assert.AreNotEqual(driveFile.Properties.FirstOrDefault(p => p.Key == "FileIdentifier").Value, GoogleDriveConverter.ComputeHashString("Repository1\\SomeFolder\\SomeFile.txt"));
            Assert.AreNotEqual(driveFile.Properties.FirstOrDefault(p => p.Key == "IsLocalRoot").Value, "False");
            return;
        }

它说"检测到意外错误"。 输出窗格也没有帮助。

  

由于执行过程意外退出,因此中止了活动的测试运行。要进一步调查,请在计算机级别或进程vstest.executionengine.x86.exe中启用本地故障转储。

顺便说一句,在您说出单元测试目的之前,我可能做错了,但我的单元测试的目的就像文档一样。我只是这样做是为了清楚地看到对哪个单位有什么期望。

1 个答案:

答案 0 :(得分:1)

您的单元测试框架版本应支持async / await。您可能需要更新到最新版本。测试方法签名必须像。

[TestMethod]
public async Task BuildDriveFileTest()
{
    ...
}

异步单元测试有nice MSDN article