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
?