How do you organise your MVC controller tests?
Posted
by Andrew Bullock
on Stack Overflow
See other posts from Stack Overflow
or by Andrew Bullock
Published on 2009-04-15T09:31:20Z
Indexed on
2010/05/27
9:01 UTC
Read the original article
Hit count: 302
unit-testing
|code-organization
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
© Stack Overflow or respective owner