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 1110265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:22:07+00:00 2026-05-17T02:22:07+00:00

I did a search on SO and looks like this question gets asked quite

  • 0

I did a search on SO and looks like this question gets asked quite often. I have been able to get the mocks working and I’m also able to execute OnActionExecuted() without any issues. Here’s my Unit Test. The commented lines are the ones that fail and I’m sure I’m not mocking the right type.

        //Arrange
        //var viewResult = new ViewResult();
        var filterContextMock = new Mock<ActionExecutedContext>();
        var routeData = new RouteData();
        var httpContextMock = new Mock<HttpContextBase>();

        routeData.Values["data"] = "Mock data";
        var requestContext = new RequestContext(httpContextMock.Object, routeData);

        var controller = new FakeController();
        controller.ControllerContext = new ControllerContext(requestContext, controller);

        filterContextMock.Setup(f => f.RouteData).Returns(routeData);
        filterContextMock.Setup(f => f.Controller).Returns(controller);
        //filterContextMock.Setup(f => f.Result).Returns(viewResult);

        //Act
        var wrapFilterAttribute = new WrapFilterAttribute();
        wrapFilterAttribute.OnActionExecuted(filterContextMock.Object);

Here is my Action Filter.

public class WrapFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var view = (ViewResultBase)filterContext.Result;

        if (view != null)
        {
            BaseViewModel viewModel = (BaseViewModel)view.ViewData.Model ?? new BaseViewModel();
            viewModel.Wrap = new WrapperFactory().GetWrap();
        }

        base.OnActionExecuted(filterContext);
    }
}

The issue I’m facing here is filterContext.Result always comes in as EmptyResult. I’d like to push in a hydrated ViewResult instead. Any ideas how I can accomplish this?

Many thanks!

  • 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-17T02:22:08+00:00Added an answer on May 17, 2026 at 2:22 am

    First let’s start by fixing your action filter as currently the code looks bad and those castings might bring you headaches:

    public class WrapFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var view = filterContext.Result as ViewResultBase;
            if (view != null)
            {
                // the controller action returned a ViewResultBase
                var viewModel = view.ViewData.Model as BaseViewModel;
                if (viewModel != null)
                {
                    // the model passed to the view was derived from
                    // BaseViewModel so we can safely update the Wrap
                    // property
                    viewModel.Wrap = new WrapperFactory().GetWrap();
                }
            }
            base.OnActionExecuted(filterContext);
        }
    }
    

    And the unit test:

    // arrange
    var sut = new WrapFilterAttribute();
    var filterContextMock = new Mock<ActionExecutedContext>();
    var viewResultMock = new Mock<ViewResultBase>();
    filterContextMock.Object.Result = viewResultMock.Object;
    var viewModel = new BaseViewModel();
    viewResultMock.Object.ViewData.Model = viewModel;
    
    // act
    sut.OnActionExecuted(filterContextMock.Object);
    
    // assert
    // TODO: assert something on the viewModel.Wrap property like 
    // for example that it has been initialized
    

    Remark: Your action filter has a strong dependency on WrapperFactory class. This is not good. A further improvement would be to abstract this functionality into an interface which would be injected into the constructor of the action filter. This would allow you further separation of concerns between different layers of your application.

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

Sidebar

Related Questions

First of all, I did a search on this and was able to find
I have a question regarding search engine optimisation. I did some research on my
Suppose I have a few lines out of wikipedia XML that looks like this:
I have an XML file that looks like this: <rss> <channel> <title>title</title> <link>link</link> <description>description</description>
Ok so I have an array that looks like this. [Enter Sandman, One, Nothing
Let's say I have a String[] that looks like this: private String[] motherOfStrings =
I did search the forum and did not find a similar question. I'm looking
Please note that this is not homework and i did search before starting this
I did a lot search already but couldn't find a straight anwser. I have
I have a Question regarding Jquery ui dialog boxes. What i have/i did: 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.