view SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserViewModelFixture.cs @ 107:572886951353

Working tests under StatLight
author stevenhollidge <stevenhollidge@hotmail.com>
date Sun, 06 May 2012 16:09:28 +0100
parents 62477c2e8837
children
line wrap: on
line source

using System;
using FluentAssertions.EventMonitoring;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SilverlightValidation.Messages;
using SilverlightValidation.Models;
using SilverlightValidation.Tests.Fakes;
using SilverlightValidation.Validators;
using SilverlightValidation.ViewModels;

namespace SilverlightValidation.Tests.ViewModels
{
    [TestClass]
    public class UserViewModelFixture
    {
        #region Constructor

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WhenConstructed_WithTwoNulls_ThenThrowsArgumentNullException()
        {
            new UserViewModel(null, null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WhenConstructed_WithNullFirstParam_ThenThrowsArgumentNullException()
        {
            new UserViewModel(null, UserModelValidator.Create());
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WhenConstructed_WithNullSecondParam_ThenThrowsArgumentNullException()
        {
            new UserViewModel(UserModel.Create(), null);
        }

        [TestMethod]
        public void WhenConstructed_WithGenericParams_ThenInstantiatesViewModel()
        {
            var vm = new UserViewModel(UserModel.Create(), UserModelValidator.Create());
            Assert.IsNotNull(vm);
        }

        #endregion

        #region INotifyPropertyChanged

        [TestMethod]
        public void WhenPropertyChanged_WithValidDateOfBirthUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();

            // act
            vm.DateOfBirth = new DateTime(1977, 01, 01);

            // assert
            vm.ShouldRaisePropertyChangeFor(x => x.DateOfBirth);
            Assert.IsTrue(vm.IsChanged);
        }


        [TestMethod]
        public void WhenPropertyChanged_WithInvalidDateOfBirthUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();

            // act
            vm.DateOfBirth = new DateTime(1800, 01, 01);

            // assert
            vm.ShouldRaisePropertyChangeFor(x => x.DateOfBirth);
            Assert.IsTrue(vm.IsChanged);
        }

        [TestMethod]
        public void WhenPropertyChanged_WithDescriptionUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();
            
            // act
            vm.Description = "New description";

            // assert
            vm.ShouldRaisePropertyChangeFor(x => x.Description);
            Assert.IsTrue(vm.IsChanged);
        }

        [TestMethod]
        public void WhenPropertyChanged_WithValidEmailUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();

            // act
            vm.Email = "test@domain.com";

            // assert
            vm.ShouldRaisePropertyChangeFor(x => x.Email);
            Assert.IsTrue(vm.IsChanged);
        }

        [TestMethod]
        public void WhenPropertyChanged_WithInvalidEmailUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();

            // act
            vm.Email = "invalid email";

            // assert
            vm.ShouldRaisePropertyChangeFor(x => x.Email);
            Assert.IsTrue(vm.IsChanged);
        }

        [TestMethod]
        public void WhenPropertyChanged_WithValidUsernameUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();

            // act
            vm.Username = "dummy";

            // assert
            vm.ShouldRaisePropertyChangeFor(x => x.Username);
            Assert.IsTrue(vm.IsChanged);
        }

        [TestMethod]
        public void WhenPropertyChanged_WithInvalidUsernameUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();

            // act
            vm.Username = "invalidusernametoolong";

            // assert
            vm.ShouldRaisePropertyChangeFor(x => x.Username);
            Assert.IsTrue(vm.IsChanged);
        }

        [TestMethod]
        public void WhenPropertyChanged_WithValidPasswordUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();

            // act
            vm.Password = "dummy";

            // assert
            vm.ShouldRaisePropertyChangeFor(x => x.Password);
            Assert.IsTrue(vm.IsChanged);
        }

        [TestMethod]
        public void WhenPropertyChanged_WithInvalidPasswordUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();

            // act
            vm.Password = "invalidpasswordtoolong";

            // assert
            vm.ShouldRaisePropertyChangeFor(x => x.Password);
            Assert.IsTrue(vm.IsChanged);
        }

        #endregion

        #region Commands

        [TestMethod]
        public void WhenNewViewModel_ThenOkCommandIsExecutable()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();

            // assert
            Assert.IsTrue(vm.OkCommand.CanExecute(null));
        }

        [TestMethod]
        public void WhenNewViewModel_ThenCancelCommandIsExecutable()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();

            // assert
            Assert.IsTrue(vm.CancelCommand.CanExecute(null));
        }

        [TestMethod]
        public void WhenNewViewModel_WithCancelExecuted_ThenShouldRaiseEvent()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();
            bool hasMessage = false;
            Messenger.Default.Register<UserViewResponseMessage>(this, message => { hasMessage = true; });

            // act
            vm.CancelCommand.Execute(null);

            // assert
            Assert.IsTrue(hasMessage);
        }
        
        #endregion

        #region Helpers

        private static UserViewModel CreateDefaultUserViewModel()
        {
            var vm = new UserViewModel(UserModel.Create(), UserModelValidatorFake.Create());
            vm.MonitorEvents();
            return vm;
        }

        #endregion
    }
}