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

  • Home
  • SEARCH
  • 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 4005400
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:23:20+00:00 2026-05-20T08:23:20+00:00

How can i write test case for action filters? I am using forms authentication.

  • 0

How can i write test case for action filters? I am using forms authentication.
I have base controller decorated with RequiresAuthentication action filter. When i execute the controller’s test case, i am not getting the loggedin user’s data from the cookies.

I am using Moq; does it provide a way to achieve my goal?

  • 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-20T08:23:21+00:00Added an answer on May 20, 2026 at 8:23 am

    This blog post from Scott Hanselmann covers MvcMockHelpers including FakeHttpContext for different mocking frameworks among others also Moq:

    using System;
    using System.Web;
    using System.Text.RegularExpressions;
    using System.IO;
    using System.Collections.Specialized;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Moq;
    
    namespace UnitTests
    {
        public static class MvcMockHelpers
        {
            public static HttpContextBase FakeHttpContext()
            {
                var context = new Mock<httpcontextbase>();
                var request = new Mock<httprequestbase>();
                var response = new Mock<httpresponsebase>();
                var session = new Mock<httpsessionstatebase>();
                var server = new Mock<httpserverutilitybase>();
    
                context.Expect(ctx => ctx.Request).Returns(request.Object);
                context.Expect(ctx => ctx.Response).Returns(response.Object);
                context.Expect(ctx => ctx.Session).Returns(session.Object);
                context.Expect(ctx => ctx.Server).Returns(server.Object);
    
                return context.Object;
            }
    
            public static HttpContextBase FakeHttpContext(string url)
            {
                HttpContextBase context = FakeHttpContext();
                context.Request.SetupRequestUrl(url);
                return context;
            }
    
            public static void SetFakeControllerContext(this Controller controller)
            {
                var httpContext = FakeHttpContext();
                ControllerContext context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
                controller.ControllerContext = context;
            }
    
            static string GetUrlFileName(string url)
            {
                if (url.Contains("?"))
                    return url.Substring(0, url.IndexOf("?"));
                else
                    return url;
            }
    
            static NameValueCollection GetQueryStringParameters(string url)
            {
                if (url.Contains("?"))
                {
                    NameValueCollection parameters = new NameValueCollection();
    
                    string[] parts = url.Split("?".ToCharArray());
                    string[] keys = parts[1].Split("&".ToCharArray());
    
                    foreach (string key in keys)
                    {
                        string[] part = key.Split("=".ToCharArray());
                        parameters.Add(part[0], part[1]);
                    }
    
                    return parameters;
                }
                else
                {
                    return null;
                }
            }
    
            public static void SetHttpMethodResult(this HttpRequestBase request, string httpMethod)
            {
                Mock.Get(request)
                    .Expect(req => req.HttpMethod)
                    .Returns(httpMethod);
            }
    
            public static void SetupRequestUrl(this HttpRequestBase request, string url)
            {
                if (url == null)
                    throw new ArgumentNullException("url");
    
                if (!url.StartsWith("~/"))
                    throw new ArgumentException("Sorry, we expect a virtual url starting with \"~/\".");
    
                var mock = Mock.Get(request);
    
                mock.Expect(req => req.QueryString)
                    .Returns(GetQueryStringParameters(url));
                mock.Expect(req => req.AppRelativeCurrentExecutionFilePath)
                    .Returns(GetUrlFileName(url));
                mock.Expect(req => req.PathInfo)
                    .Returns(string.Empty);
            }
        }
    }
    

    Another good resource for ASP.NET MVC test helpers is the MvcContrib project on CodePlex.

    After you have a FakeHttpContext you can test your action filter:

    var context = new ActionExecutedContext();
    context.HttpContext = MvcMockHelpers.FakeHttpContext();
    context.Result = new EmpytResult(); // or whatever the default result should be
    
    var filter = new MyCustomAttribute();
    filter.OnActionExecuted(context);
    
    Assert.True(context.Result is EmpytResult);
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using copy-on-write semantics to share memory among processes, how can you test if
I have an action filter which i got from the below link http://blog.wekeroad.com/blog/aspnet-mvc-securing-your-controller-actions/ there
I can write: update my_table set xml = updateXML(xml, '/a/b', '1') where document_id =
In SQL one can write a query that searches for a name of a
I know that with mysql you can write SQL statements into a .sql file
Is there a way I can write text to a file from a certain
In Eclispe you can do Ctrl+Shift+R and a Window popup where you can write
I'm writing an app where 3rd party vendors can write plugin DLLs and drop
In PHP, I can write: $vname = 'phone'; $$vname = '555-1234'; print $phone; ...
What is the least amount of code you can write to create, sort (ascending),

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.