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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:12:10+00:00 2026-05-16T06:12:10+00:00

I’m trying to unit test a custom action result. I recently watched Jimmy Bogard’s

  • 0

I’m trying to unit test a custom action result. I recently watched Jimmy Bogard’s excellent MvcConf video (“put your controllers on a diet”) http://www.viddler.com/explore/mvcconf/videos/1/ and have started to try and implement some custom action results. I’ve managed that without a problem, the ActionResult works fine at runtime but I’m having trouble trying to unit test them.

Unfortunately in the code download there are no unit tests for Jimmy’s custom action methods… which make me wonder.

I realise that action methods just return instances of the ActionResult types and its the MVC framework that actually calls the ExecuteResult method, which of course is not available when running the unit test. So my unit test is now just creating an instance of my custom ActionResult and I then call ExecuteResult.

Unfortunatley in the ExecuteResult method of my custom ActionResult it is also calling the ExecuteResult method of a ViewResult that I passed it. At that point it blows up. How should I be mocking/stubbing these things to get my unit test working?

public class SendToAFriendActionResult : ActionResult
{

    public const string INVALID_CAPTCHA = "You don't appear to have filled out the two words from the security image correctly to prove you're a human. Please try again.";
    public const string INVALID_MODEL_STATE = "You don't appear to have filled out all the details correctly. Please try again.";
    public const string CONTACT_FAIL = "Unfortunately we experiend a problem sending the link. Please try again later.";
    public const string SEND_TO_A_FRIEND_FAIL_KEY = "ContactFail";

    private  RedirectResult _success;
    private  ViewResult _failure;
    private readonly SendToAFriendModel _model;
    private readonly bool _captchaValid;
    private readonly MessageBuilderServiceBase _mbs;

    public RedirectResult Success
    {
        get { return _success; }
        set { _success = value; }
    }

    public ViewResult Failure
    {
        get { return _failure; }
        set { _failure = value; }
    }

    public SendToAFriendActionResult(RedirectResult success, ViewResult failure, SendToAFriendModel model, bool captchaValid, MessageBuilderServiceBase mbs)
    {
        _success = success;
        _failure = failure;
        _model = model;
        _captchaValid = captchaValid;
        _mbs = mbs;
    }

    public override void ExecuteResult(ControllerContext context)
    {

        if (!_captchaValid)
        {
            Failure.TempData[SEND_TO_A_FRIEND_FAIL_KEY] = INVALID_CAPTCHA;

            // On reaching this point I receive the error
            // Object reference not set to an instance of an object
            // as the MVC framework calls FindView 
            Failure.ExecuteResult(context);
            return;
        }

        if (!context.Controller.ViewData.ModelState.IsValid)
        {
            Failure.TempData[SEND_TO_A_FRIEND_FAIL_KEY] = INVALID_MODEL_STATE;
            Failure.ExecuteResult(context);
            return;
        }

        _mbs.RecipientEmailAddress = _model.EmailRecipient;
        _mbs.SendersName = _model.SendersName;
        _mbs.Url = _model.URL;
        var result = _mbs.sendMessage();

        if (!result)
        {
            Failure.TempData[SEND_TO_A_FRIEND_FAIL_KEY] = CONTACT_FAIL;
            Failure.ExecuteResult(context);
            return;
        }

        Success.ExecuteResult(context);
    }
}

Here’s the start of my unit test …

        IMessageService _emailMessageSerivce;
        IGalleryRepository _repository;

        var stfModel = new SendToAFriendModel
        {
            SendersName = "Someone",
            URL = "http://someurl.com",
            EmailRecipient = "a-friend@somewherelse.com"
        };

        var failure = new ViewResult() {ViewName ="SendToFriend"};
        const bool captchaValid = false;
        var fakeControlllerContext = MockRepository.GenerateStub<ControllerContext>(null);

        var stf = new SendToAFriendActionResult(null, failure, stfModel, captchaValid, null);
        stf.ExecuteResult(fakeControlllerContext);

I’ve put comments in the SUT to show were the problem occurs.

I know I should be stubbing/mocking somehow but I just can’t seem to resolve this.

  • 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-16T06:12:11+00:00Added an answer on May 16, 2026 at 6:12 am

    From ASP.NET MVC 2 In Action (coauthored by Jimmy Bogard):

    By taking that hard-to-test code out
    of an action and putting it into the
    Execute method of an action result,
    you ensure that the actions become
    significantly easier to unit-test.
    That’s because when you unit-test an
    action, you assert the type of action
    result that the action returns and the
    state of the action result. The
    Execute method of the action result
    isn’t executed as part of the unit
    test.

    Unit tests are designed to isolate behavior and concerns. You’re mixing concerns by calling ExecuteResult from within your custom Action. Instead, I would have the SendToAFriendActionResult return the actual ActionResult (Failure or Success):

    public ActionResult GetAction(..)
    {
       ActionResult result;
       //logic here to determine which ActionResult to return
       return result;
    }
    

    In your Controller:

      public ViewResult SendToAFriend()
        {
           return SendToAFriendActionResult(null, failure, stfModel, captchaValid, null)
                .GetAction();
        }
    

    This method will allow the MVC framework to do its job and isolates those concerns outside your custom ActionResult. Your test should assert that the correct type of Action, failure or success, is returned based on the parameters you set going in.

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.