view SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserViewModelTests.cs @ 98:d0c2cac12376

Latest version
author stevenhollidge <stevenhollidge@hotmail.com>
date Sat, 05 May 2012 22:53:40 +0100
parents 1adc1ae981ea
children
line wrap: on
line source

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

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

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

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

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

        #endregion

        #region INotifyPropertyChanged

        [Test]
        public void PropertyChange_WhenDateOfBirthUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
        {
            // arrange
            var vm = CreateDefaultUserViewModel();
            vm.MonitorEvents();

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

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

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

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

        #endregion

        #region INotifyDataErrorInfo

        #endregion

        #region Commands

        #endregion

        #region Helpers

        private static UserViewModel CreateDefaultUserViewModel()
        {
            var vm = new UserViewModel(new UserModel(), new UserModelValidator());
            return vm;
        }

        #endregion
    }
}