10

我正在考虑将我的一些 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);
}

你认为你的单元测试应该如何构建?你当然可以在你的测试方法中调用异步启动器方法,但是你如何获得返回值呢?

我在谷歌上没有看到任何关于这个的...

感谢您的任何想法。

4

2 回答 2

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
}

希望有帮助:)

于 2010-06-04T10:27:43.673 回答
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);
    }
}

如果您想了解更多信息,我已经写了一篇关于如何使用 Machine.Specifications 对 ASP.NET MVC 3 异步控制器进行单元测试的博客文章, 或者如果您想检查此代码,它位于github

于 2012-09-23T11:17:53.103 回答