本文介绍了单元测试中的LiveAuthClient.LoginAsync错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对我的Live Connect代码运行一些单元测试,但在登录时失败。

I'm trying to run some unit test on my Live Connect code but it fails at login.

代码:

// Open Live Connect SDK client.
LiveAuthClient LCAuth = new LiveAuthClient();
LiveLoginResult LCLoginResult = await CAuth.InitializeAsync();

try
{
LiveLoginResult loginResult = null;
loginResult = await LCAuth.LoginAsync(new string[] { "wl.basic", "wl.photos", "wl.skydrive", "wl.skydrive_update" });

// ... some more code here
}

我得到以下LiveAuthException:exception = {"发生错误在执行操作时。请稍后再试。使用InnerException"}

I get the following LiveAuthException: exception = {"An error occurred while performing the operation. Please try again later."}

  {" Element not found.\r\\\
"}  HResult  0x80070032

with InnerException {"Element not found.\r\n"} HResult 0x80070032

堆栈跟踪:

" at Windows.Security.Authentication.OnlineId.OnlineIdAuthenticator.AuthenticateUserAsync(IEnumerable`1 requests,CredentialPromptType credentialPromptType)\\\\ n  在Microsoft.Live.TailoredAuthClient。< GetAccessToken> d__b.MoveNext()\\\\ nn ---
从抛出异常的上一个位置开始的堆栈跟踪结束--- \\\\ n  &NBSP;在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r\ n  在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)\ r\ n  在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\\ nn  在Microsoft.Live.TailoredAuthClient。< AuthenticateAsync> d__0.MoveNext()"

"at Windows.Security.Authentication.OnlineId.OnlineIdAuthenticator.AuthenticateUserAsync(IEnumerable`1 requests, CredentialPromptType credentialPromptType)\r\n   at Microsoft.Live.TailoredAuthClient.<GetAccessToken>d__b.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at Microsoft.Live.TailoredAuthClient.<AuthenticateAsync>d__0.MoveNext()"

我认为这是因为我无法访问我的身份验证令牌,但有人有解决方案可以运行这样的代码在单元测试中?

I assume that this is because I cannot access my authentication token, but does someone has a solution to run such code in a unit test?

谢谢

Stephane

推荐答案

如果要对应用程序进行单元测试,则应将身份验证抽象为接口。 您的实时代码将使用LiveAuthClient,但您的单元测试将使用模拟返回您想要测试应用程序
如何处理成功/失败方案的任何响应。

If you want to unit test your application, you should abstract away authentication into an interface.  Your live code would use the LiveAuthClient, but your unit tests would use a mock that returns whatever responses you want to test how your application handles success/failure scenarios.


这篇关于单元测试中的LiveAuthClient.LoginAsync错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 21:09