Search code examples
unit-testingcode-organization

How do you organise your MVC controller tests?


I'm looking for tidy suggestions on how people organise their controller tests.

For example, take the "add" functionality of my "Address" controller,

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Add()
{
    var editAddress = new DTOEditAddress();
    editAddress.Address = new Address();
    editAddress.Countries = countryService.GetCountries();

    return View("Add", editAddress);
}

[RequireRole(Role = Role.Write)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add(FormCollection form)
{
    // save code here
}

I might have a fixture called "when_adding_an_address", however there are two actions i need to test under this title...

I don't want to call both actions in my Act() method in my fixture, so I divide the fixture in half, but then how do I name it?

"When_adding_an_address_GET" and "When_adding_an_address_POST"?

things just seems to be getting messy, quickly.

Also, how do you deal with stateless/setupless assertions for controllers, and how do you arrange these wrt the above? for example:

[Test]
public void the_requesting_user_must_have_write_permissions_to_POST()
{
    Assert.IsTrue(this.SubjectUnderTest.ActionIsProtectedByRole(c => c.Add(null), Role.Write));
}

This is custom code i know, but you should get the idea, it simply checks that a filter attribute is present on the method. The point is it doesnt require any Arrange() or Act().

Any tips welcome!

Thanks


Solution

  • Here's what I do now:

    /tests/controllers/address/add/get.cs
    /tests/controllers/address/add/valid.cs
    /tests/controllers/address/add/invalid.cs