In this tutorial, you will learn how to validate that any redirect code you add within your Episerver CMS-powered controllers works as expected. This will be done by writing a unit test. During an Episerver build, it is very likely that during a request, you may want to redirect a user to another page. For example, after logging in. In order to redirect a user, you can still use RedirectToAction with Episerver, however, you will also need to add the page id within the node segment in order for the redirect to work successfully. This sounds great, how do we write a unit test to prove it works? Below I will answer how you can test this code 🔥🔥🔥

In this example, I'm using FluentAssertions so don't freak out over the different syntax 😉. In the test, you can get access to the ActionResultobject. Next, you will need to type it as a 'RedirectToRouteResult'. After that, you can then look in the RouteValues collection for the node segment. You can then check that the value of node contains the page ID you expect!

Testing Response.Redirect

You can also redirect a visitor using a Url within your controller. In your unit test, you can validate that the Url is correct using the RedirectResult objects URL property:


You are now a redirect testing Ninja 🐱‍👤. Happy Coding 🤘