none
VS11 UnitTest Code, cannot access Document folder in metro app。 RRS feed

  • 问题

  • I write a sample used the Unit Test,  and want to access KnownFolders.DocumentsLibrary, but system throw execption 'System.UnauthorizedAccessException'. And I have add capabilities in UnitTest Project Package.appxmanifest. 
    2012年4月19日 10:20

答案

  • Well, I got it, please add the async key word on the TestMethod. Then the testing can be passed.


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年4月24日 4:54
    版主
  • Well, I got the feedback from product team. The problem is that we should specify the “File type associations” in the application manifest also, at least add one File Type, for example, we could add one as .txt as below screenshot:

    then the test method can work as expected with/without async keyword.

    --> Why add async keyword can pass the previous testing?

    The testing will always be passed with the async keyword. The difference is where the assert exception is being thrown when async is used.  If we have a TestMethod that returns void then internally we’re using something called an AsyncVoidMethodBuilder which  “saves” the assert fail exception on the thread pool. So for the TestMethod, it seems pass.

    When marking a method with async, classes like AsyncVoidMethodBuilder intercept the exception but without async then the exception is captured by the assembly Microsoft.VisualStudio.TestPlatform.Extensions.VSTestIntegration which can handle the exception naturally.   


    More FAQ about Async/Await, please refer to: http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年4月26日 10:23
    版主

全部回复

  • Could you please tell how do you code your Unit Test application, do you try to access one file in the KnownFolders.DocumentsLibrary? Please show some code here.

    I follow the document, it can run and get one file from my document library: http://msdn.microsoft.com/en-us/library/windows/apps/hh441482(v=vs.110).aspx

    P.S Note the remarks and cautions in the above document.


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年4月23日 7:17
    版主
  • [TestMethod]
            public void TestFileStore2()
            {
                MFilesStore store = new MFilesStore("test", KnownFolders.DocumentsLibrary);
                store.createFile("test.txt");
                bool ret = store.isFileExists("test.txt");
                Assert.IsTrue(ret);

                store.renameFile("test.txt", "t.txt");
                ret = store.isFileExists("t.txt");
                Assert.IsTrue(ret);

                store.delFile("t.txt");
                ret = store.isFileExists("t.txt");
                Assert.IsFalse(ret);
                ret = store.isFileExists("test.txt");
                Assert.IsFalse(ret);
            }

    It just like haven't add capability in project, but I do it in all appxmanifest files.

    2012年4月24日 2:18


  • hi wallace zeng,you need to be modified appxmanifest files,  documents Library must be ticked

    2012年4月24日 3:37
  • I write a simple code to test the case, like this:

    [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                StorageFolder filefolder = KnownFolders.DocumentsLibrary;
                Assert.IsTrue(filefolder != null);
            }
        }

    and will throw exception above, and I have modified appxmanifest.

    2012年4月24日 3:50
  • Well, I got it, please add the async key word on the TestMethod. Then the testing can be passed.


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年4月24日 4:54
    版主
  • why need async? Is not this key work should use with await for synchronization?
    2012年4月24日 5:47
  • The async method should have one or more await, but that is only a warning, we could declare the async method and without any await invocation.

    And for why this method should have the async keyword, I have consulted it with the product team, and any feedback I will update here, thank you.


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年4月25日 3:40
    版主
  • Well, I got the feedback from product team. The problem is that we should specify the “File type associations” in the application manifest also, at least add one File Type, for example, we could add one as .txt as below screenshot:

    then the test method can work as expected with/without async keyword.

    --> Why add async keyword can pass the previous testing?

    The testing will always be passed with the async keyword. The difference is where the assert exception is being thrown when async is used.  If we have a TestMethod that returns void then internally we’re using something called an AsyncVoidMethodBuilder which  “saves” the assert fail exception on the thread pool. So for the TestMethod, it seems pass.

    When marking a method with async, classes like AsyncVoidMethodBuilder intercept the exception but without async then the exception is captured by the assembly Microsoft.VisualStudio.TestPlatform.Extensions.VSTestIntegration which can handle the exception naturally.   


    More FAQ about Async/Await, please refer to: http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年4月26日 10:23
    版主