0

I have a mock (using Moq) setup for a Mvc3 controller eg.

[TestMethod]
public void Step_One_Post_Should_Redirect_To_Step_Two()
{
    // this test checks that when I submit a type parameter of StepOneModel and
    // the next button was clicked Request["next"] ,  then goto Step Two
}

The only thing stopping this test running is the Save Method called from the controller, which fails as values aren't properly set in Session in this test context. Really I want to omit this database call from this unit test.

Basically how do I stup/mock the Save(model) call from the controller action?

[HttpPost]
public ActionResult Index(IStepViewModel step)
{
    // after model is deserialized
    if (!string.IsNullOrEmpty(Request["next"]))
    {
        Save(model)  <-- the bit blocking me
    }

    return View(model)  <-- the bit I want to assert against
}
2
  • Wouldn't it be easier to push the bits that require testing back into the Model, and test it there? Commented Nov 19, 2012 at 21:36
  • Yes @RobertHarvey, it will be easier, but it won't test the controller. You need to check that the controller do save the model when that action is called. Of course, there are other ways to check it with mocking the ORM saving process, but he still need to mock the session for couple of reasons. Commented Nov 21, 2012 at 9:46

1 Answer 1

1

I suggest using the excellent library MvcContrib-test helpers

It gives you mocked session out of the box.

General Concepts

One of the major advantages of the new MVC.Net framework is the ease with which it can be tested. While this is generally true, there are a number of areas where testing the framework becomes difficult to do. The TestHelper assists by providing a Controller Factory that creates controllers with internal data members correctly initialized. These include:

  • HttpContext
  • HttpRequest
  • HttpResponse
  • HttpSession <-----------------------
  • Form
  • TempData
  • QueryString
  • ApplicationPath
  • PathInfo
Sign up to request clarification or add additional context in comments.

1 Comment

@MikeW, No problem dude, I was glad to help. I don't need the rep, but... if you find the answer helpful, why didn't you upvoted it? not a rep whore...

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.