我正在考虑重新编写一些我的MVC控制器作为异步控制器。我有这些控制器的工作单元测试,但我试图了解如何在异步控制器环境中维护它们。
例如,目前我有这样的行动:
public ContentResult Transaction()
{
do stuff...
return Content("result");
}
我的单元测试基本上是这样的:
var result = controller.Transaction();
Assert.AreEqual("result", result.Content);
好的,这很容易。
但是当你的控制器变成这样时:
public void TransactionAsync()
{
do stuff...
AsyncManager.Parameters["result"] = "result";
}
public ContentResult TransactionCompleted(string result)
{
return Content(result);
}
您如何构建单元测试?您当然可以在测试方法中调用异步启动器方法,但是如何获得返回值?
我在谷歌上没有看到任何相关内容...
感谢任何想法。
答案 0 :(得分:18)
与任何异步代码一样,单元测试需要了解线程信令。 .NET包含一个名为AutoResetEvent的类型,它可以阻止测试线程,直到完成异步操作:
public class MyAsyncController : Controller
{
public void TransactionAsync()
{
AsyncManager.Parameters["result"] = "result";
}
public ContentResult TransactionCompleted(string result)
{
return Content(result);
}
}
[TestFixture]
public class MyAsyncControllerTests
{
#region Fields
private AutoResetEvent trigger;
private MyAsyncController controller;
#endregion
#region Tests
[Test]
public void TestTransactionAsync()
{
controller = new MyAsyncController();
trigger = new AutoResetEvent(false);
// When the async manager has finished processing an async operation, trigger our AutoResetEvent to proceed.
controller.AsyncManager.Finished += (sender, ev) => trigger.Set();
controller.TransactionAsync();
trigger.WaitOne()
// Continue with asserts
}
#endregion
}
希望有所帮助:)
答案 1 :(得分:1)
我编写了简短的AsyncController扩展方法,简化了单元测试。
static class AsyncControllerExtensions
{
public static void ExecuteAsync(this AsyncController asyncController, Action actionAsync, Action actionCompleted)
{
var trigger = new AutoResetEvent(false);
asyncController.AsyncManager.Finished += (sender, ev) =>
{
actionCompleted();
trigger.Set();
};
actionAsync();
trigger.WaitOne();
}
}
这样我们就可以简单地隐藏线程'噪音':
public class SampleAsyncController : AsyncController
{
public void SquareOfAsync(int number)
{
AsyncManager.OutstandingOperations.Increment();
// here goes asynchronous operation
new Thread(() =>
{
Thread.Sleep(100);
// do some async long operation like ...
// calculate square number
AsyncManager.Parameters["result"] = number * number;
// decrementing OutstandingOperations to value 0
// will execute Finished EventHandler on AsyncManager
AsyncManager.OutstandingOperations.Decrement();
}).Start();
}
public JsonResult SquareOfCompleted(int result)
{
return Json(result);
}
}
[TestFixture]
public class SampleAsyncControllerTests
{
[Test]
public void When_calling_square_of_it_should_return_square_number_of_input()
{
var controller = new SampleAsyncController();
var result = new JsonResult();
const int number = 5;
controller.ExecuteAsync(() => controller.SquareOfAsync(number),
() => result = controller.SquareOfCompleted((int)controller.AsyncManager.Parameters["result"]));
Assert.AreEqual((int)(result.Data), number * number);
}
}
如果你想了解更多,我写了一篇关于如何Unit test ASP.NET MVC 3 asynchronous controllers using Machine.Specifications的博客文章 或者,如果您想查看此代码,请访问github