I've been trying to find some straightforward information on this, but I haven't been able to - either what I've found has been to vague for me to understand what's going on, or too specific for the wrong thing, for example a tutorial I found for unit testing an AuthorizeAttribute. (A third alternative is of course that I'm too dumb to realize that I'm reading the information I'm looking for... in which case I still need help, because I'm dumb :P)
I'd like to be able to test two things:
And I'm clueless. Could anyone push me in the right direction as to what I actually need to test? What should my Arrange, Act and Assert sections of the tests contain?
A little detail, if I'm not clear enough:
I have a CustomValidationFilter
that is supposed to check if two submitted form values are the same, and add a ModelStateError
if not. I want to verify that the error is added with the correct error message if the values are not the same, and that the error is not added if the values are equal (and non-null).
I also have a Write
action on a GuestbookController
. I want to verify that the filter is applied to this action.
Using reflection in your test.
var method = typeof(MyController).GetMethod("MyMethod");
var attribute = method.GetCustomAttributes(typeof(CustomValidationFilter),false);
.Cast<CustomValidationFilter>()
.SingleOrDefault();
Assert.IsNotNull( attribute );
Assert.AreEqual( "value", atttribute.SomeProperty );
Create a unit test for the method in your class. Mock up the filterContext with the appropriate data and check that whatever variables the method is supposed to set, are in fact what you expect them to be.
public void AttributeTest()
{
var httpContext = MockRepository.GenerateMock<HttpContextBase>();
var controller = new FakeController();
controller.controllerContext = new ControllerContext( httpContext, new RouteData(), controller );
...
attribute.OnActionExecuting( filterContext );
Assert...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With