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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:34:37+00:00 2026-05-28T02:34:37+00:00

I’m working with a team that have had problems with random failing tests. The

  • 0

I’m working with a team that have had problems with random failing tests. The failing tests seemingly only failed on the buildserver (TFS). But as I investigated the problem I was able to have them fail on my local machine as well.

It seems that I have fixed the problem, but I need confirmation of my theory about the failing tests.

One of the tests that failed had this TestInitialize method:

    [TestInitialize]
    public void TestInit()
    {
        Mock<ILog> loqMock = new Mock<ILog>();
        Depend.RegisterInstance(loqMock.Object);
    }

The Depend class is a static wrapper around the Unity container. It has a local reference to the Unity container inside it. Some tests in this fixture fails seemingly at random.

My theory is that the logMock variable is dereferenced, since there is no reference to either that or the Depend class after the Initialize method returns.

I “fixed” the problem by changing the code to this:

    private Mock<ILog> loqMock;

    [TestInitialize]
    public void TestInit()
    {
        loqMock = new Mock<ILog>();
        Depend.RegisterInstance(loqMock.Object);
    }

Now the logMock variable is referenced as long as the testfixture lives. I cannot get it to fail, as I could with the other setup.

PS: I know that this use of an IOC container is a really bad idea and that it should be changed, but the project I’m consulting, doesn’t have the resources to make the change, and there’s nothing I can do about it.

  • 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-28T02:34:37+00:00Added an answer on May 28, 2026 at 2:34 am

    Jand187,
    You’re 100% right that the loqMock object is being gathered up by the garbage collector. You’re only registering the object which is being created by your Mock, which doesn’t contain a reference back to the Mock object itself.

    One way you can verify this is to use the setup where it fails occasionally, and before executing the line where it would fail do a GC.Collect(). After invoking the GC, it should fail every time.

    If you want the object which defined and initialized within the TestInit() method of your unit test to live for the duration of it, you’ll have to ensure at least one reference to it is kept within “some” object which will have a lifecycle equal to or longer than your the duration of the test – in this case, your “fix” (quoting quotes) is absolutely correct.

    If you’re interested in the why and how, here’s a link to a very useful writeup by MS covering Garbage Collection relatively deeply: http://msdn.microsoft.com/en-us/library/ee787088.aspx

    Good luck!

    Adam

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.