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.
We tried to add some previous object initialization, before executing 'CanExecute' and sometimes it worked (or we thought that).
testedViewModel.Object.InEditMode = inEditMode;
I have a test:
[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<SomeViewModel>(inEditMode)
{
CallBase = true
};
testedViewModel.Setup(x => x.InEditMode).Returns(inEditMode);
Assert.AreEqual(expectedResult, testedViewModel.Object.Command.CanExecute(null));
}
Sometimes (NOT ALWAYS) when Jenkins does the build and run unit tests some of can execute tests failed with message:
MESSAGE:
Expected: True
But was: False
+++++++++++++++++++
STACK TRACE:
at Project.CommandCanExecute(Boolean inEditMode, Boolean expectedResult)
The problem is that is happening only on Jenkins and it's very nondeterministic.
EDIT:
Ok, one more thing to think about. Property InEditMode is placed in base parent class of SomeModelView.
And I merged code for you in the sample.
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*/, () => InEditMode);
}
}
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.
SOLUTION
I've created an attribute class:
[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 => ActionTargets.Test;
}
And then I can use for each 'CanExecute' test this attribute:
[GarbageCollectorDisabler]
[TestCase(TestName = "SomeTest_InEditMode_CanExecute")]
public void SomeTestCanExecute()
{}
MESSAGE: Expected: True But was: False
the test you have showed inputsfalse
every time for the expected result, so how could the expected betrue
in the message you showed us? – GlovskyCanExecute
method on the command – GlovskyInEditMode
is able to be overridden in your example when it is notvirtual
? Provide a minimal reproducible example that can be used to reproduce the problem, allowing a better understanding of what is being asked. – Infinitive