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