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 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 have this code inside a header (edited): template <int i> class A {};
I have edited this post with my question writen in a more simple way
I have edited this question but at the bottom of the program I have
I have edited this question to make it easier to understand. I have an
I have edited this OS 3.2 question based on advice that it could infringe
I have this code that I've edited: http://pastebin.com/vrqHek6S I've put in comments where I
I have the following Models and ViewModels (edited for brevity): public class Advert {
I have the following on a web servlet: EDITED: public String tryGoogleAuthentication(String auth_token){ HttpURLConnection
--edited for clarity (hopefully) I have an XML file that looks something like this:
I have edited the question as what I said before was two pieces of

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.