0

I am writing some integration tests for an MVC web application, which involves firing http post and get requests at an instance of the application that I spin up with IIS Express.

The problem is that I want to assert that validation has passed (for example on account creation), and obviously this is impossible to interpret from just validation messages mixed in with markup that is returned (well not impossible, but out of the question).

What I am currently doing is checking the ModelState in my controller base class in the OnActionExecuted event and setting the status code to a 202 (Accepted but still requires processing):

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
     if (!ModelState.IsValid)
     {
          Response.StatusCode = 202;
     }

     base.OnActionExecuted(filterContext);
}

I know 202 doesn't really reflect what has happened, but there isn't a status code that fits the situation, and it won't cause an error on the browser when the application is in normal use.

What are peoples opinions of this design? And will it have any adverse affects?

0

2 Answers 2

2

Why don't you test just the controller? There should be no need to test the IIS part of the stack.

If you really want to do, i think hidden fields filled with the data you want to verify are a better approach.

Sign up to request clarification or add additional context in comments.

1 Comment

I want to test the application running because otherwise testing just the controller requires me to replicate all configuration files in my test project (it is a large project so has a lot, and I also want to test configuration works), also these are integration tests so I want to test things like authorisation cookies (logging in etc).
0

Oh well...looks like most peoples' opinions seem to be 'don't care' or 'don't know'.

Just in case people are interested I have decided against using the status code, as it is a bit hacky, I just add a header to the response instead, and do an assert on that:

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    if (!ModelState.IsValid)
    {
        Response.AddHeader("Validation", "Failed");
    }

    base.OnActionExecuted(filterContext);
}

Comments

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.