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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:36:01+00:00 2026-05-15T02:36:01+00:00

I have edited and simplified this question a lot. If I have this method

  • 0

I have edited and simplified this question a lot.

If I have this method on my HomeController:

    public ActionResult Strangeness( int id )
    {
        StrangenessClass strangeness = null;

        if( id == 1 )
        {
            strangeness = new StrangenessClass() { Name="Strangeness", Desc="Really weird behavior" };
        }

        return View( strangeness );
    }

And have this class:

public class StrangenessClass
{
    public string Name { get; set; }
    public string Desc { get; set; }
}

Why does this unit test fail?

    [TestMethod]
    public void Strangeness()
    {
        HomeController controller = new HomeController();

        ViewResult result = controller.Strangeness( 1 ) as ViewResult;
        var model = result.ViewData.Model;
        result = controller.Strangeness( 2 ) as ViewResult;
        model = result.ViewData.Model;

        Assert.IsNull( model );
    }

I understand that normally, I would have one test to test the null condition and another to test a good condition, but I ran into this problem while testing my delete controller. On a delete test, I would normally fetch the record, delete the record, and then attempt to fetch it again. It should be null the second time I fetch it, but it wasn’t. So, I boiled the problem down as described above.

If this is not the proper way to test deletes, how would you do it? Don’t you need to make sure that the record was actually deleted?

  • 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-15T02:36:01+00:00Added an answer on May 15, 2026 at 2:36 am

    You should not reuse a controller to handle multiple requests, which is exactly what you are doing here.

    Anyway, if you check the source code for MVC you’ll find the reason for this behavior:

    protected internal virtual ViewResult View(string viewName, string masterName, object model)
    {
        if (model != null)
        {
            base.ViewData.Model = model;
        }
        return new ViewResult { ViewName = viewName, MasterName = masterName, ViewData = base.ViewData, TempData = base.TempData };
    }

    If the model is null, it’s not assigned to the ViewData.Model property.
    If you want the correct behaviour, create a new controller for your second call to HomeController.Strangeness.

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

Sidebar

Related Questions

I originally posted a much simpler (and less helpful) question, and have edited this
I have edited the original question since the same error is occurring the difference
I have found this example on StackOverflow: var people = new List<Person> { new
I have HTML code edited by FCKEditor stored in a database and would like
I have a JavaScript resource that has the possibility of being edited at any
Have you managed to get Aptana Studio debugging to work? I tried following this,
I think I have not explained correctly. So i have just edited the previous
My assignment is to fix the code. I have my edited code below and
I have a new web app that is packaged as a WAR as part
i have edited an app and made some changes in it. I want to

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.