我得到了一个用回调
调用方法的UnitTest方法 [Test]
public void GetUserAsyncTest()
{
User result;
_restTest.GetUserAsync((user) =>
{
result = user;
});
Assert.AreEqual("xy", result.Email);
}
这是我的方法签名
/// <summary>
/// Retrieve the User details for the currently authenticated User
/// </summary>
/// <param name="callback">Method to call upon successful completion</param>
public void GetUserAsync(Action<User> callback)
如何测试这个并从回调中获取价值?目前我的结果始终为null,这是合乎逻辑的。
答案 0 :(得分:3)
使用事件等待异步方法完成:
[Test]
public void GetUserAsyncTest()
{
//Action<User> user = null;
User result;
ManualResetEvent waitEvent = new ManualResetEvent(false);
_restTest.GetUserAsync((user) =>
{
result = user;
waitEvent.Set();
});
waitEvent.WaitOne();
Assert.AreEqual("xy", result.Email);
}
还将user.Email
更改为result.Email
。怀疑您要查看result
变量。