view SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserViewModelFixture.cs @ 102:db05a55e3536

INPC tests added
author stevenhollidge <stevenhollidge@hotmail.com>
date Sun, 06 May 2012 12:13:29 +0100
parents SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserViewModelTests.cs@d0c2cac12376
children 62477c2e8837
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 Helpers

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

        #endregion
    }
}