Failure is inevitable

In all of the SpecsFor.Mvc examples I’ve posted so far, I’ve omitted one common cross-cutting requirement of web applications: authentication! Most web apps have some sort of authentication, and we need to be able to test our core application logic without this cross-cutting concern getting in the way. SpecsFor.Mvc makes it easy to achieve exactly that, as I’ll show you in this post.

That introduces another inheritance requirement though and still makes our specs more complicated than they need to be. Let’s look at the right way to tackle this.

Authentication the SpecsFor.Mvc Way

SpecsFor.Mvc includes a simple way to handle authentication. During configuration, you can specify a handlerthat will be used to authenticate with the application at the beginning of each spec. Let’s update the SpecsForMvcConfig in our AssemblyStartup to specify an authentication handler:

Now we can write specs against controllers that are not accessible to unauthenticated users without authentication concerns obscuring our specs.

What’s Next?

You now know how to create and configure a handler for authentication, but how do you setup the credentials to use for authentication? You could manually configure the database your app uses during integration testing, but SpecsFor.Mvc actually provides another solution. In the next post, we’ll look at how you can load seed data into your application for use during your integration tests. In the meantime, the package is on NuGet, and the code is on Github. Enjoy!

About Matt Honeycutt...

Matt Honeycutt is a software architect specializing in ASP.NET web applications, particularly ASP.NET MVC. He has over a decade of experience in building (and testing!) web applications.
He’s an avid practitioner of Test-Driven Development, creating both the SpecsFor and SpecsFor.Mvc frameworks.

He's also an author for Pluralsight,
where he publishes courses on everything from web applications to testing!