积极答复者
VS11 UnitTest Code, cannot access Document folder in metro app。

问题
答案
-
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
- 已建议为答案 Jie BaoModerator 2012年4月24日 4:55
- 已标记为答案 Jie BaoModerator 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
- 已标记为答案 Jie BaoModerator 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
-
[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.
-
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.
-
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
- 已建议为答案 Jie BaoModerator 2012年4月24日 4:55
- 已标记为答案 Jie BaoModerator 2012年4月25日 3:40
-
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
-
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
- 已标记为答案 Jie BaoModerator 2012年4月26日 10:23