4

I want to write a TaskController for an ASP.NET MVC 3 application to some long running tasks, like sending a newsletter to the users of the site. I thought using an AsyncController would be appropriate as sending emails might take a while, and I want to be able to save some state to the database when the task finishes running.

Being the properly brought up developer that I am (:þ), and being really into BDD, I naturally want to start off with a spec using MSpec.

Imagine my controller looks like this:

public class TaskController : AsyncController
{    
    readonly ISession _session;

    public TaskController(ISession session)
    {
        _session = session;
    }

    public void SendMailAsync()
    {
        // Get emails from db and send them
    }

    public ActionResult SendMailCompleted()
    {
        // Do some stuff here
    }
}

How does one go about writing specs for AsyncControllers? Imagine I start with the following specification:

public class TaskControllerContext
{
    protected static Mock<ISession> session;
    protected static TaskController controller;
    protected static ActionResult result;
}

[Subject(typeof(TaskController), "sending email")]
public class When_there_is_mail_to_be_sent : TaskControllerContext
{
    Establish context = () =>
    {
        session = new Mock<ISession>();
        controller = new TaskController(session.Object);
    };

    // is this the method to call?
    Because of = () => controller.SendMailAsync();

    // I know, I know, needs renaming...
    It should_send_mail;
}

Should I be calling the SendMailAsync method for the test? I actually feels yucky. How do I deal with the result from SendMailCompleted?

4

1 回答 1

3

好吧,我终于想出了至少一种方法来做到这一点。我敢肯定还有其他可能更好的方法,但这里有:

声明一个AutoResetEvent将用作等待句柄和一个EventHandler将设置为在异步操作完成时触发的。然后在Because块中(对应于单元测试的部分Act)调用:WaitOneAutoResetEvent

static AutoResetEvent waitHandle; 
static EventHandler eventHandler;
static int msTimeout = 5000;
static IEnumerable<Email> response;

Establish context = () =>
{
    toSend = new List<Email>
    {
        // Emails here
    };

    // Prepare the mock objects
    session.Setup(x => x.All<Email>()).Returns(toSend.AsQueryable());
    mailer.Setup(x => x.SendMail(Moq.It.IsAny<IEnumerable<Email>>()))
        .Returns(toSend);

    // Set up the wait handle    
    waitHandle = new AutoResetEvent(false);
    eventHandler = (sender, e) => waitHandle.Set();
    controller.AsyncManager.Finished += eventHandler;
};

Because of = () =>
{
    controller.SendMailAsync();
    if (!waitHandle.WaitOne(msTimeout, false)) {}

    response = (IEnumerable<Email>) controller.AsyncManager
        .Parameters["sentMails"];
};

It should_send_mail = () => response.Any().ShouldBeTrue();

如果您使用 NUnit 或任何其他测试框架而不是 MSpec 编写单元测试,这应该也可以。

于 2011-04-03T10:03:25.973 回答