Unit testing a view model

Topics: Getting Started
Mar 22, 2013 at 8:06 PM
Edited Mar 23, 2013 at 8:31 PM
Lets say I have the following view model. How can I write a unit test class for it?
I could not find an example in the documentation
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Globalization;

using Caliburn.Micro;

namespace MY_PROJECT.ViewModels
{   
    [Export(typeof(MainViewModel))]
    public class MainViewModel : Screen
    {     
        private readonly IWindowManager _windowManager = null;

        private readonly IEventAggregator _events;
        
        private string _title = string.Empty;          
      
        private WindowState _windowState = WindowState.Normal;
       
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
                NotifyOfPropertyChange(() => Title);
            }
        }

        public WindowState WindowState
        {
            get
            {
                return _windowState;
            }
            set
            {
                _windowState = value;
                NotifyOfPropertyChange(() => WindowState);
            }
        }

       [ImportingConstructor]
       public MainViewModel(IWindowManager windowManager, IEventAggregator events)
        {
            _windowManager = windowManager;
            _events = events;
            _events.Subscribe(this);
        }
}
Apr 8, 2013 at 5:04 AM
Well, you can test this model like any other class. You have to instantiate MainViewModel, invoke a method and assert something)))
Besides, I don't see any code that should be tested in your example. Trivial properties? For what? Well, for the sake of truth I have to say that a question of a trivial props testing is a hollywar question.
Apr 8, 2013 at 10:04 AM
I got a reply about that here
I guess I should have provided a more complex example, but my suggestion would be that there should be a unit test example in the examples folder in caliburn micro source.