PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/MVVM RI/MVVM.Client.Tests/ViewModels/OpenQuestionViewModelFixture.cs

#
C# | 108 lines | 74 code | 17 blank | 17 comment | 0 complexity | b079ce702a241715d565e1d1e85834cf MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System;
 18using System.Linq;
 19using Microsoft.VisualStudio.TestTools.UnitTesting;
 20using MVVM.Client.ViewModels;
 21using MVVM.Questionnaires.Model;
 22using MVVM.TestSupport;
 23
 24namespace MVVM.Client.Tests.ViewModels
 25{
 26    [TestClass]
 27    public class OpenQuestionViewModelFixture
 28    {
 29        [TestMethod]
 30        public void WhenCreatingAViewModelWithANullQuestion_ThenAnExceptionIsThrown()
 31        {
 32            try
 33            {
 34                new OpenQuestionViewModel(null);
 35                Assert.Fail("should have thrown");
 36            }
 37            catch (ArgumentNullException)
 38            {
 39                // expected
 40            }
 41        }
 42
 43        [TestMethod]
 44        public void ViewModelHasMaxLengthAsTheAvailableLength()
 45        {
 46            var question = new OpenQuestionTemplate { QuestionText = "Question", MaxLength = 25 }.CreateNewQuestion() as OpenQuestion;
 47            var viewModel = new OpenQuestionViewModel(question);
 48
 49            Assert.AreEqual(25, viewModel.AvailableLength);
 50        }
 51
 52        [TestMethod]
 53        public void WhenSettingTheResponseTextPropertyOnTheModel_ThenAvailableLengthIsUpdatedOnTheViewModel()
 54        {
 55            var question = new OpenQuestionTemplate { QuestionText = "Question", MaxLength = 25 }.CreateNewQuestion() as OpenQuestion;
 56            var viewModel = new OpenQuestionViewModel(question);
 57
 58            question.Response = "1234567890";
 59
 60            Assert.AreEqual(15, viewModel.AvailableLength);
 61        }
 62
 63        [TestMethod]
 64        public void WhenSettingTheResponseTextPropertyOnTheModel_ThenAChangeOnAvailableLengthIsNotifiedOnTheViewModel()
 65        {
 66            var question = new OpenQuestionTemplate { QuestionText = "Question", MaxLength = 25 }.CreateNewQuestion() as OpenQuestion;
 67            var viewModel = new OpenQuestionViewModel(question);
 68
 69            var changeTracker = new PropertyChangeTracker(viewModel);
 70            question.Response = "1234567890";
 71
 72            Assert.IsTrue(changeTracker.ChangedProperties.Contains("AvailableLength"));
 73        }
 74
 75        [TestMethod]
 76        public void WhenSettingTheResponseTextPropertyOnTheModel_ThenTheViewModelNotifiesAResponseChange()
 77        {
 78            var question = new OpenQuestionTemplate { QuestionText = "Question", MaxLength = 25 }.CreateNewQuestion() as OpenQuestion;
 79            var viewModel = new OpenQuestionViewModel(question);
 80
 81            bool responseChanged = false;
 82            viewModel.ResponseChanged += (s, e) => responseChanged = true;
 83            question.Response = "1234567890";
 84
 85            Assert.IsTrue(responseChanged);
 86        }
 87
 88        [TestMethod]
 89        public void WhenCreatingTheViewModel_ThenHasChangesIsFalse()
 90        {
 91            var question = new OpenQuestionTemplate { QuestionText = "Question", MaxLength = 25 }.CreateNewQuestion() as OpenQuestion;
 92            var viewModel = new OpenQuestionViewModel(question);
 93
 94            Assert.IsFalse(viewModel.HasChanges);
 95        }
 96
 97        [TestMethod]
 98        public void WhenSettingTheResponseOntheModel_ThenViewModelHasChanges()
 99        {
100            var question = new OpenQuestionTemplate { QuestionText = "Question", MaxLength = 25 }.CreateNewQuestion() as OpenQuestion;
101            var viewModel = new OpenQuestionViewModel(question);
102
103            question.Response = "1234567890";
104
105            Assert.IsTrue(viewModel.HasChanges);
106        }
107    }
108}