jeudi 28 avril 2016

Nsubstitue mock framework void function

 [TestFixture]
    public class AlgorithmTest
    {

        private ProgressIndicatorViewModel n_progressIndicatorViewModel;

        [SetUp]

        public void InitMock()
        {            
            n_progressIndicatorViewModel = Substitute.For<ProgressIndicatorViewModel>();
        }
       [Test]
        public void RunAlgoRegression()
        {
            ConfigurationViewModel _configurationViewModel = new ConfigurationViewModel(n_progressIndicatorViewModel);
              _configurationViewModel.ExceuteCreateNewProjectCommand();
              _configurationViewModel.ExecuteMoveNextStepCommand();

Here is the code of the class I'm testing

internal async void ExecuteMoveNextStepCommand()
        {
            if (CurrentStepIndex < WizardSteps.Count - 1)
            {
                CurrentStep = WizardSteps[CurrentStepIndex + 1];
                this.CurrentStep.Init();
                MovePreviousStepCommand.RaiseCanExecuteChanged();
            }
            else
            {
                var result = _messageBoxService.Show("Do you want to create a table and exit the wizard?", "Warning", MessageBoxButton.OKCancel);
                if (result == MessageBoxResult.OK)
                {
                    //Exception is here
                    _progressIndicatorViewModel.Start(this, "Start Creating Possible Configurations..."); 
                    await Task.Run(() => Algorithm());
                    _progressIndicatorViewModel.Stop(this);
                }
            }
        }

Here is Start() function

public void Start(object owner, string message = "Working...", bool isIntermediate = false, Action stopAction = null, Action pauseAction = null)
        {
            if (!IsActive)
            {
                Owner = owner;    
            }
            else
            {
                string infoMsg = "Multiple starts for progress bar: " + owner + " Tried to start progress bar which was already started by" + Owner;
                IPDevLoggerWrapper.Info(infoMsg);
                Console.WriteLine(infoMsg);
            }
            IsFocused = true;
            Message = message;
            IsActive = true;
            Progress = 0.01;
            IsIntermediate = isIntermediate;
            HasOperationBeenStopped = false;
            StopAction = stopAction; //Here is the Exception 
            HasOperationBeenPaused = false;
            PauseAction = pauseAction;
            RaisePropertyChanged(() => IsErrorsFound);
        }

I thought since this is a void function the mock will just do whatever it needs to do and mock the class for me. however when I call the ProgressIndicatorViewModel from a UnitTest I get the exception

A first chance exception of type 'System.NullReferenceException' occurred in CalibrationBase.dll

Am I miss using NSubstitude?

Aucun commentaire:

Enregistrer un commentaire