locked
How to test async function with Unit test

    Question

  • I have a function code like this:

            public static async Task<bool> RemoveUnitFile(string unitType)
            {
                if (!unitType.ToLower().EndsWith(".xml"))
                {
                    unitType = unitType + ".xml";
                }
                StorageFolder local = ApplicationData.Current.LocalFolder;
                try
                {
                    StorageFile file = await local.GetFileAsync(unitType);
                    await file.DeleteAsync();
                    return true;
                }
                catch
                {
                    return false;
                }
            }

    And I'm trying to create a Unit Test function:

            [TestMethod]
            public async void TestDeleteFile()
            {
                Assert.IsTrue(await DataAccess.RemoveUnitFile("Units"));
            }

    But after I build the Test project, the test method TestDeleteFile is not exist in the test explorer, how can I test the async functions??

    Sunday, March 30, 2014 8:25 AM

Answers

  • change it to:

    [TestMethod]
            public void TestDeleteFile()
            {
                Assert.IsTrue(DataAccess.RemoveUnitFile("Units").Result);
            }


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Sunday, March 30, 2014 2:47 PM