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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:41:40+00:00 2026-05-11T20:41:40+00:00

I want to write a unit test to ensure that the view I am

  • 0

I want to write a unit test to ensure that the view I am returning is the correct one.

My plan is to write a test that first invokes the controller and then calls the ActionResult method I plan to test
I thought I could write something like

Controller controller = new HomeController();
var actionresult = controller.Index();
Assert.False(actionresult.ToString(), String.Empty);

which would then allow me to parse the actionresult for the test value.
However I cannot directly instantiate the public ActionResult Index() method.

How do I do 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-11T20:41:40+00:00Added an answer on May 11, 2026 at 8:41 pm

    Here’s an example from Professional ASP.NET MVC 1.0 (book):

    
    [TestMethod]
    public void AboutReturnsAboutView()
    {
         HomeController controller = new HomeController();
         ViewResult result = controller.About() as ViewResult;
    
         Assert.AreEqual("About", result.ViewName);
    }
    

    Note that this will fail if you don’t return an explicit view in your controller method, i.e. do this:

    
         Return(View("About"));
    

    not this:

    
         Return(View());
    

    Or the test won’t pass. You should only need to do this if your method will ever return more than one view, otherwise you should return an implicit view anyway and not bother testing the framework.

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

Sidebar

Related Questions

I want to write a unit test for a Django manage.py command that does
Greetings, I want to write a unit test to make sure that our web
I want to write a unit test that performs HTTP requests directly (instead of
This is a function that I want to write a unit test for: public
I am trying to write a unit test using .NET 4 to ensure that
I want to write a unit test which tests the function of a class
Assume I want to write a unit test to test a particular piece of
I want to write a few unit tests that do not make any changes
I want to write unit test for my classes but I cannot solve linking
I have some Web API methods that I want to write unit tests for.

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.