view SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserViewModelFixture.cs @ 106:62477c2e8837

Command tests
author stevenhollidge <stevenhollidge@hotmail.com>
date Sun, 06 May 2012 13:00:46 +0100
parents db05a55e3536
children 572886951353
line wrap: on
line source

using System;
using FluentAssertions.EventMonitoring;
using NUnit.Framework;
using SilverlightValidation.Models;
using SilverlightValidation.Tests.Fakes;
using SilverlightValidation.Validators;
using SilverlightValidation.ViewModels;

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

        [Test]
        public void WhenConstructed_WithTwoNulls_ThenArgumentNullExceptionForModel()
        {
            Assert.Throws<ArgumentNullException>(() => new UserViewModel(null, null), "model");
        }

        [Test]
        public void WhenConstructed_WithNullFirstParam_ThenArgumentNullExceptionForModel()
        {
            Assert.Throws<ArgumentNullException>(() => new UserViewModel(null, UserModelValidator.Create()), "model");
        }

        [Test]
        public void WhenConstructed_WithNullSecondParam_ThenArgumentNullExceptionForValidator()
        {
            Assert.Throws<ArgumentNullException>(() => new UserViewModel(UserModel.Create(), null), "validator");
        }

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

        #endregion

        #region INotifyPropertyChanged

        [Test]
        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);
        }


        [Test]
        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);
        }

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

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

        [Test]
        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);
        }

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

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

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

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

            // act
            vm.Username = "dummy";

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

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

            // act
            vm.Username = "invalidusernametoolong";

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

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

            // act
            vm.Password = "dummy";

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

        [Test]
        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

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

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

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

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

        [Test]
        public void WhenNewViewModel_WithCancelExecuted_ThenShouldRaiseEvent()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();

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

            // assert
            vm.ShouldRaise("UserViewResponseMessage");
        }
        
        #endregion

        #region Helpers

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

        #endregion
    }
}