Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7218835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:32:13+00:00 2026-05-28T21:32:13+00:00

I have an MSpec test to check whether my forms auth is correctly redirecting

  • 0

I have an MSpec test to check whether my forms auth is correctly redirecting an unauthorised request, however the test call to the protected action just goes straight to it without getting caught by the authorisation.
From what I’ve read people usually need to fake authentication to test actions behing the [Authorize] tag, so I don’t understand how it’s just going straight to the protected action method.

If anyone could help it would be much appreciated, this is my first attempt at using MSpec and it looks like it should be really useful, I just can’t get it to work!

Controller:

[Authorize]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View("Index", null);
    }
}

Test:

[Subject("Login and Authentication")]
public class when_protected_page_invoked
{
    private static HomeController homeController;
    private static SecurityController securityController;
    private static ActionResult result;

    private Establish context = () =>
    {
        homeController = new HomeController();
        securityController = new SecurityController(new SecurityService(new NHibernateRepository<User>(), new NHibernateRepository<Session>()));
    };

    private Because of = () => result = homeController.Index();

    private It should_redirect_to_securityController = () =>
        {
            result.ShouldBeARedirectToRoute().And().ControllerName().ShouldEqual("Security");
        };
}

When I run the test at the moment it fails with an exception that a ViewResult is being returned, and if I debug it’s just returning the Home.Index() result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:32:14+00:00Added an answer on May 28, 2026 at 9:32 pm

    That’s normal. Action filters are not executed in this case. All you do is to call the action method in your unit test. The proper way to unit test this is to verify that this controller is decorated with the Authorize attribute:

    Assert.IsTrue(typeof(HomeController).GetCustomAttributes(typeof(AuthorizeAttribute), true).Any());
    

    The fact that when a controller is decorated with the Authorize attribute will redirect to the proper login page if the user is not authneticated is not something that you should unit test. That’s part of the ASP.NET MVC framework which Microsoft (hopefully) have already extensively unit tested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started to use MSpec for BDD, and since long ago I use
I have the following spec (using Machine.Specifications or mSpec): public class when_a_user_logs_in_successfully { static
I have been using MSpec to write my unit tests and really prefer the
I have mspec running with R#, but my client doesn't have R# and doesn't
I'm trying to write an MSpec test that instantiates one of my controllers with
I have about 4-6 years .NET experience. Primarly using ASP.NET MVC/Nhibernate/Castle Windsor/MSpec/etc in visual
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.