mmssaann
mmssaann

Reputation: 1507

Unit test HttpPost Create action of MVC4 controller

Can somebody please guide me how to write Unit test in VS 2012 unit test project to the httppost create action?

My action looks like:

[HttpPost]
    public ActionResult Create(Organization obj)
    {
        if (ModelState.IsValid)
        {
            OrganizationRepo.Create(obj);
            UnitOfWork.Save();
            return RedirectToAction("List");
        }
        else
        {
            return View();
        }
    }

After successfully creating an object in database, I am not sure what to assert in unit tests..

It would be great if you can suggest some negative tests also...

Thanks in advance..

Upvotes: 1

Views: 734

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

After successfully creating an object in database, I am not sure what to assert in unit tests

You should assert that the controller action redirected to the List action, i.e. the ActionResult returned is a RedirectToRouteResult.

It would be great if you can suggest some negative tests also

In this case you could assert that no object has been created and the same Create view was rendered, i.e. the returned ActionResult is a ViewResult.

Upvotes: 3

Related Questions