title: Jenkins failed unit CanExecute test's methods nondeterministic
body: <p>We have a lot CanExecute tests for a various commands in our project.
  All tests passed properly when we use Visual Studio testing or AxoCover.</p>
  
  <p>We tried to add some previous object initialization, before executing 'CanExecute' and sometimes it worked (or we thought that).</p>
  
  <pre><code>testedViewModel.Object.InEditMode = inEditMode;
  </code></pre>
  
  <p>I have a test:</p>
  
  <pre><code>[TestCase(true, true, TestName = "Command_InEditMode_CanExecute")]
  [TestCase(false, false, TestName = "Command_NotInEditMode_CannotExecute")]
  public void CommandCanExecute(bool inEditMode, bool expectedResult)
  {
      var testedViewModel =
          new Mock&lt;SomeViewModel&gt;(inEditMode)
          {
              CallBase = true
          };
  
      testedViewModel.Setup(x =&gt; x.InEditMode).Returns(inEditMode);
  
      Assert.AreEqual(expectedResult, testedViewModel.Object.Command.CanExecute(null));
  }
  </code></pre>
  
  <p>Sometimes (NOT ALWAYS) when Jenkins does the build and run unit tests some of can execute tests failed with message:</p>
  
  <pre><code>MESSAGE:
    Expected: True
    But was:  False
  
  +++++++++++++++++++  
  STACK TRACE:
     at Project.CommandCanExecute(Boolean inEditMode, Boolean expectedResult)
  </code></pre>
  
  <p>The problem is that is happening only on Jenkins and it's very nondeterministic.</p>
  
  <p>EDIT:</p>
  
  <p>Ok, one more thing to think about. Property InEditMode is placed in base parent class of SomeModelView.</p>
  
  <p>And I merged code for you in the sample.</p>
  
  <pre><code>public BaseViewModel 
  {
      public virtual bool InEditMode {get; set;}
  }
  
  public SomeViewModel : BaseViewModel
  {
      public SomeViewModel () : base ()
      {
  
      }
  
      public ICommand Command { get; set; }
  
      public virtual void RegisterCommands()
      {
          Command = new RelayCommand(/*Do something*/, () =&gt; InEditMode);
      }
  }
  </code></pre>
  
  <p>And we think that can be related, that object is thinking that is initialized before initialization of base class is done. But that is very hard to check this with a Jenkins.</p>
  
  <hr>
  
  <h2>SOLUTION</h2>
  
  <p>I've created an attribute class:</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Assembly, AllowMultiple = true)]
  public class GarbageCollectorDisabler : Attribute, ITestAction
  {
      public void BeforeTest(ITest test)
      {
          GC.TryStartNoGCRegion(2048 * 4096);
      }
  
      public void AfterTest(ITest test)
      {
          GC.EndNoGCRegion();
      }
  
      public ActionTargets Targets =&gt; ActionTargets.Test;
  }
  </code></pre>
  
  <p>And then I can use for each 'CanExecute' test this attribute:</p>
  
  <pre><code>[GarbageCollectorDisabler]
  [TestCase(TestName = "SomeTest_InEditMode_CanExecute")]
  public void SomeTestCanExecute()
  {}
  </code></pre>