PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
C# | 136 lines | 91 code | 21 blank | 24 comment | 0 complexity | afc3107c1944484bd43dddbcd32ce5f9 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 Microsoft.VisualStudio.TestTools.UnitTesting;
 19using MVVM.Client.ViewModels;
 20using MVVM.Questionnaires.Model;
 21
 22namespace MVVM.Client.Tests.ViewModels
 23{
 24    [TestClass]
 25    public class NumericQuestionViewModelFixture
 26    {
 27        [TestMethod]
 28        public void WhenCreatingAViewModelWithANullQuestion_ThenAnExceptionIsThrown()
 29        {
 30            try
 31            {
 32                new NumericQuestionViewModel(null);
 33                Assert.Fail("should have thrown");
 34            }
 35            catch (ArgumentNullException)
 36            {
 37                // expected
 38            }
 39        }
 40
 41        [TestMethod]
 42        public void WhenResponseChangesOnTheModel_ThenResponseValueChangeIsFired()
 43        {
 44            var question = new NumericQuestionTemplate { MaxValue = 100 }.CreateNewQuestion() as NumericQuestion;
 45            var viewModel = new NumericQuestionViewModel(question);
 46
 47            bool responseChangeFired = false;
 48            viewModel.ResponseChanged += (s, e) => { responseChangeFired = true; };
 49            question.Response = 15;
 50
 51            // Assertions
 52            Assert.IsTrue(responseChangeFired);
 53        }
 54
 55        [TestMethod]
 56        public void WhenHasErrorIsChangedToTrueOnTheViewModel_ThenResponseValueChangeIsFired()
 57        {
 58            var question = new NumericQuestionTemplate { MaxValue = 100 }.CreateNewQuestion() as NumericQuestion;
 59            var viewModel = new NumericQuestionViewModel(question);
 60
 61            viewModel.HasBindingError = false;
 62            bool responseChangeFired = false;
 63            viewModel.ResponseChanged += (s, e) => { responseChangeFired = true; };
 64            viewModel.HasBindingError = true;
 65
 66            // Assertions
 67            Assert.IsTrue(responseChangeFired);
 68        }
 69
 70        [TestMethod]
 71        public void WhenHasErrorIsChangedToFalseOnTheViewModel_ThenResponseValueChangeIsFired()
 72        {
 73            var question = new NumericQuestionTemplate { MaxValue = 100 }.CreateNewQuestion() as NumericQuestion;
 74            var viewModel = new NumericQuestionViewModel(question);
 75
 76            viewModel.HasBindingError = true;
 77            bool responseChangeFired = false;
 78            viewModel.ResponseChanged += (s, e) => { responseChangeFired = true; };
 79            viewModel.HasBindingError = false;
 80
 81            // Assertions
 82            Assert.IsTrue(responseChangeFired);
 83        }
 84
 85        [TestMethod]
 86        public void WhenResponseIsSetOnTheModel_ThenTheHasErrorPropertyInTheViewModelIsCleared()
 87        {
 88            var question = new NumericQuestionTemplate { MaxValue = 100 }.CreateNewQuestion() as NumericQuestion;
 89            var viewModel = new NumericQuestionViewModel(question);
 90
 91            viewModel.HasBindingError = true;
 92            int responseChanges = 0;
 93            viewModel.ResponseChanged += (s, e) => { responseChanges++; };
 94            question.Response = 15;
 95
 96            // Assertions
 97            Assert.IsFalse(viewModel.HasBindingError);
 98            Assert.AreEqual(1, responseChanges);
 99        }
100
101        [TestMethod]
102        public void WhenCreatingANewViewModel_ThenHasChangesIsFalse()
103        {
104            var question = new NumericQuestionTemplate { MaxValue = 100 }.CreateNewQuestion() as NumericQuestion;
105            var viewModel = new NumericQuestionViewModel(question);
106
107            // Assertions
108            Assert.IsFalse(viewModel.HasChanges);
109        }
110
111        [TestMethod]
112        public void WhenResponseIsSetOnTheModel_ThenHasChangesIsTrue()
113        {
114            var question = new NumericQuestionTemplate { MaxValue = 100 }.CreateNewQuestion() as NumericQuestion;
115            var viewModel = new NumericQuestionViewModel(question);
116
117            question.Response = 15;
118
119            // Assertions
120            Assert.IsTrue(viewModel.HasChanges);
121        }
122
123        [TestMethod]
124        public void WhenViewModelHasErrorIsSet_ThenDoesNotHaveCompletedResponse()
125        {
126            var question = new NumericQuestionTemplate { MaxValue = 100 }.CreateNewQuestion() as NumericQuestion;
127            var viewModel = new NumericQuestionViewModel(question);
128
129            question.Response = 15;
130            viewModel.HasBindingError = true;
131
132            // Assertions
133            Assert.IsFalse(viewModel.ResponseComplete);
134        }
135    }
136}