Hulikoi
Hulikoi

Reputation: 43

How to execute C# wpf commands via a unit test

I'm fairly new to unit tests in general and C#, I'm trying to verify that once a command funs that a collection has the correct number of objects. So the flow should be: Command excecutes -> method adds objects to collection -> Test checks collection has objects. When I try this the test throws the error "System.InvalidOperationException: The calling thread must be STA, because many UI components require this."

Since the command works fine in normal opperation I assume there is something about the way I have set up the testing that is incorrect. I Tried adding [STAThread] attribute to the testing method but still got the same error.

My ViewModel to test

namespace Space
{
    public class SampleViewModel : ObservableObject, IPageViewModel
    {        
        private string _id;
        private string _initials;
        private ObservableCollection<ISampleObject> _sampleModels;
        private ICommand _validateId;
        private IdValidator _idValidator;
      
        public ViewModel(string initials)
        {
            Initials = initials;            
        }

        public string Name
        {
            get { return "Sample"; }
        }

        public string Id
        {
            get
            {
                return _id;
            }
            set
            {
                if (value != _id)
                {
                    _id = value;
                    OnPropertyChanged("Id");
                }
            }
        }

        public ObservableCollection<ISampleObject> SampleModels
        {
            get
            {
                if (_sampleModels == null)
                {
                    _sampleModels = new ObservableCollection<ISampleObject>();
                    OnPropertyChanged("SampleModels");
                }
                return _sampleModels;
            }
            set
            {

            }
        }

        public IdValidator IdValidator
        {
            get
            {
                if (_idValidator == null)
                {
                    _idValidator = new IdValidator();
                    OnPropertyChanged("IdValidator");
                }
                return _idValidator;
            }
            set { }
        }

        public ICommand ValidateIdCommand
        {
            get
            {
                if (_validateId == null)
                {
                    _validateId = new RelayCommand(
                        param => ValidateId(AliquotId),
                        Predicate => AliquotId != ""
                    );
                }
                return _validateId;
            }
        }

        /**
         * <summary> This method checks the entered sample ID and attempts to categorise it 
         * and call the correct follow-up methods.
         * </summary>
         **/        
        private void ValidateId(string Id)  // potentailly this should return a bool on successful/unsuccessful validation to make testing easier.
        {
            if (Id != null)
            {   
                if (IdValidator.IsValidId(Id))
                {                    
                    switch (IdValidator.ValidateId(AliquotId))
                    {
                        case IdType.Sample:
                            GetSample(Id);
                            break;                        
                        default:
                            break;
                    }
                }
                else
                {
                    MessageBox.Show("Scanned Code not recognised", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

private void GetSample(string id)
        {
            SampleProvider sampleProvider = new SampleProvider();
            
            SampleModel sampleModel = (SampleModel)sampleProvider .GetById(id);
            if (!sampleModel.IsEnumPlate)
            {
                SampleModels.Add(sampleModel);
            }            
        }

    }
}

The testing code

namespace Space.Tests
{
    [TestClass()]
    public class SampleViewModelTests
    {
        ViewModel viewModel = new ViewModel("string") { 
            Id = "ID string"
        };

        [TestMethod()]
        public void ViewModelTest_SampleModel_Retrieved()
        {
            viewModel.ValidateIdCommand.Execute(viewModel.Id);
            Assert.IsTrue(viewModel.SampleModels.Count > 0);
        }
    }
}
'''

Upvotes: 0

Views: 1181

Answers (1)

mm8
mm8

Reputation: 169190

Calling Execute is how you invoke the command:

viewModel.ValidateIdCommand.Execute(viewModel.Id);

So far so good.

As commented by @KlausGütter, you should mock the call to MessageBox.Show to get this working.

A common and easy approach is to create a dialog service that implements an interface:

public interface IDialogService
{
    void ShowError(string text, string caption);
}

public class DialogService : IDialogService
{
    public void ShowError(string text, string caption) =>
        MessageBox.Show(text, caption, MessageBoxButton.OK, MessageBoxImage.Error);
}

...and inject the view model with the interface:

private readonly IDialogService _dialogService;
public ViewModel(string initials, IDialogService dialogService)
{
    Initials = initials;
    _dialogService = dialogService ?? throw new ArgumentNullException(nameof(dialogService));
}


private void ValidateId(string Id)  // potentailly this should return a bool on successful/unsuccessful validation to make testing easier.
{
    if (Id != null)
    {
        if (IdValidator.IsValidId(Id))
        {
            switch (IdValidator.ValidateId(AliquotId))
            {
                case IdType.Sample:
                    GetSample(Id);
                    break;
                default:
                    break;
            }
        }
        else
        {
            _dialogService.Show("Scanned Code not recognised", "Error");
        }
    }
}

Upvotes: 1

Related Questions