2

I have a method on MVC5 controller:-

[HttpPost]
public JsonResult Save(TaskViewModel model)
{
    if (ModelState.IsValid)
    {
        var task = model.ToTask();
        _taskService.Save(task);
        return Json(task);
    }

    return Json(new ErrorViewModel(ModelState));
}

Which I am happily unit testing like so:-

[Test]
public void Save_WhenInvalidModel_ThenDoNotCallITaskServiceSave()
{
    var model = new TaskViewModel();
    var service = new Mock<ITaskService>();
    service.Setup(m => m.Save(It.IsAny<Task>()));

    var controller = CreateController(service.Object);

    ValidateModel(model, controller);

    controller.Save(model);

    service.Verify(f => f.Save(It.IsAny<Task>()), Times.Never());
}

protected static void ValidateModel(object model, Controller controller)
{
    var validationContext = new ValidationContext(model, null, null);
    var validationResults = new List<ValidationResult>();
    Validator.TryValidateObject(model, validationContext, validationResults);

    foreach (var validationResult in validationResults)
    {
        controller.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
    }
}

However, I need to make this method return a 400 status code and return content. So I made the following change to the controller method.

[HttpPost]
public JsonResult Save(TaskViewModel model)
{
    if (ModelState.IsValid)
    {
        var task = model.ToTask();
        _taskService.Save(task);
        return Json(task);
    }

    Response.StatusCode = 400;
    Response.TrySkipIisCustomErrors = true;
    return Json(new ErrorViewModel(ModelState));
}

This causes my unit test to fail with a System.NullReferenceException because Response does not exist.

So my question is what is the best practice here to both make this controller testable and also to verify the status code value?

UPDATE While this is very similar to MVC3 unit testing response code it does not address that I am trying to return JSON content as well as a 400 status code

2
  • 1
    possible duplicate of MVC3 unit testing response code Commented Feb 20, 2014 at 15:15
  • I have already read that question. While that is very close. I want to return a JSONResult as well as a 400 response code. So it is actually pretty different Commented Feb 20, 2014 at 15:28

1 Answer 1

0

Have a look at this answer. It explains in detail how to test using HttpContext and Response.

Sign up to request clarification or add additional context in comments.

3 Comments

Please use the flag as duplicate option instead of posting a link to another question as an answer.
@cadrell0 how does just flagging it as duplicate help me get an answer though?
@baynezy It points you to the other question that has an answer. If your question is not a true duplicate of the one suggested, then explain why it is different, which you have already done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.