UnitTest.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using EducationDepartament.Views;
  2. using EducationDepartment.Models;
  3. using EducationDepartment.ViewModels;
  4. using Microsoft.EntityFrameworkCore;
  5. using Moq;
  6. using UnitTestHelpers;
  7. namespace EducationDepartment.test
  8. {
  9. [TestClass]
  10. public class UnitTest
  11. {
  12. [TestMethod]
  13. public void TestOnPropertyChanged_UpdatesValues()
  14. {
  15. // Arrange
  16. var dbContextMock = new Mock<Schekaleva31pContext>(); // Creating a mock of the database context
  17. var viewModel = new AttendanceViewModel(dbContextMock.Object); // Creating an instance of the AttendanceViewModel
  18. bool eventRaised = false; // Flag to track if the PropertyChanged event was raised
  19. // Subscribing to the PropertyChanged event to check for changes in the 'Values' property
  20. viewModel.PropertyChanged += (sender, e) =>
  21. {
  22. if (e.PropertyName == nameof(viewModel.Values))
  23. {
  24. eventRaised = true;
  25. }
  26. };
  27. // Act
  28. viewModel.SecondSelectedOption = "SomeGroup"; // Triggering a property change
  29. // Assert
  30. Assert.IsTrue(eventRaised); // Verifying that the PropertyChanged event was raised for the 'Values' property
  31. }
  32. [TestMethod]
  33. public void TestAuthorizationViewModel_LoginPropertyChange()
  34. {
  35. // Arrange
  36. var viewModel = new AuthorizationViewModel(); // Creating an instance of the AuthorizationViewModel
  37. bool eventRaised = false; // Flag to track if the PropertyChanged event was raised
  38. // Subscribing to the PropertyChanged event to check for changes in the 'Login' property
  39. viewModel.PropertyChanged += (sender, e) =>
  40. {
  41. if (e.PropertyName == nameof(viewModel.Login))
  42. {
  43. eventRaised = true;
  44. }
  45. };
  46. // Act
  47. viewModel.Login = "newlogin@example.com"; // Triggering a property change
  48. // Assert
  49. Assert.IsTrue(eventRaised); // Verifying that the PropertyChanged event was raised for the 'Login' property
  50. }
  51. [TestMethod]
  52. public void AttendanceViewModel_GetNewAttendance_SetsCorrectParameterForDisciplineFilter()
  53. {
  54. // Arrange
  55. var dbConnect = new Schekaleva31pContext(); // Creating a mock of the database context
  56. var attendanceViewModel = new AttendanceViewModel(dbConnect); // Creating an instance of the AttendanceViewModel
  57. attendanceViewModel.SelectedOption = "Äèñöèïëèíà"; // Setting the SelectedOption
  58. attendanceViewModel.OptionValue = "Âñå ãðóïïû"; // Setting the OptionValue
  59. // Act
  60. attendanceViewModel.GetNewAttendance(); // Invoking the GetNewAttendance method
  61. // Assert
  62. Assert.AreEqual("Ãðóïïû íà äèñöèïëèíå ", attendanceViewModel.ParameterName); // Verifying that the ParameterName is set correctly
  63. }
  64. [TestMethod]
  65. public void AttendanceViewModel_NewOption_ReturnsCorrectOptionsForGroupFilter()
  66. {
  67. // Arrange
  68. var dbConnect = new Schekaleva31pContext(); // Creating a mock of the database context
  69. var attendanceViewModel = new AttendanceViewModel(dbConnect); // Creating an instance of the AttendanceViewModel
  70. attendanceViewModel.SelectedOption = "Ãðóïïà";// Setting the SelectedOption
  71. // Act
  72. var options = attendanceViewModel.NewOption(); // Invoking the NewOption method
  73. // Assert
  74. Assert.IsNotNull(options); // Verifying that the options are not null
  75. Assert.IsTrue(options.Count > 0); // Verifying that the options list has at least one item
  76. }
  77. [TestMethod]
  78. public void TimetableViewModel_NewValues_ForGroup_ReturnsExpectedValues()
  79. {
  80. // Arrange
  81. var dbContext = new Schekaleva31pContext(); // Creating a mock of the database context
  82. var timetableViewModel = new TimetableViewModel(dbContext); // Creating an instance of the TimetableViewModel
  83. timetableViewModel.SelectedOption = "Ãðóïïà"; // Setting the SelectedOption
  84. var expectedValuesCount = 40; // Defining the expected number of values
  85. // Act
  86. var values = timetableViewModel.NewValues("Ãðóïïà"); // Invoking the NewValues method with a specific group
  87. // Assert
  88. Assert.AreEqual(expectedValuesCount, values.Count); // Verifying that the count of values matches the expected count
  89. }
  90. [TestMethod]
  91. public void TimetableViewModel_NewTimetable_ForTeacher_ReturnsExpectedTimetable()
  92. {
  93. // Arrange: Create a mock object for Schekaleva31pContext
  94. var dbContext = new Schekaleva31pContext();
  95. // Create an instance of TimetableViewModel with the mock context
  96. var timetableViewModel = new TimetableViewModel(dbContext);
  97. // Set the SelectedOption to "Teacher" (Ïðåïîäàâàòåëü in Russian)
  98. timetableViewModel.SelectedOption = "Ïðåïîäàâàòåëü";
  99. // Define the expected count of timetable entries
  100. var expectedTimetableCount = 0;
  101. // Set the OptionValue to a specific teacher's name
  102. timetableViewModel.OptionValue = "John Doe";
  103. // Act: Call the NewTimetable method to populate the Timetable property
  104. timetableViewModel.NewTimetable();
  105. // Assert: Verify that the count of timetable entries matches the expected count
  106. Assert.AreEqual(expectedTimetableCount, timetableViewModel.Timetable?.Count);
  107. }
  108. [TestMethod]
  109. public void DisciplineViewModel_NewValues_ForGroup_ReturnsExpectedValues()
  110. {
  111. // Arrange: Create a mock object for Schekaleva31pContext
  112. var dbContext = new Schekaleva31pContext();
  113. // Create an instance of DisciplineViewModel with the mock context
  114. var disciplineViewModel = new DisciplineViewModel(dbContext);
  115. // Set the SelectedOption to "Group" (Ãðóïïà in Russian)
  116. disciplineViewModel.SelectedOption = "Ãðóïïà";
  117. // Define the expected count of values
  118. var expectedValuesCount = 40;
  119. // Act: Call the NewValues method with the "Group" parameter and set the Values property
  120. disciplineViewModel.Values = disciplineViewModel.NewValues("Ãðóïïà");
  121. // Assert: Verify that the count of values matches the expected count
  122. Assert.AreEqual(expectedValuesCount, disciplineViewModel.Values.Count);
  123. }
  124. [TestMethod]
  125. public void DisciplineViewModel_NewDisciplines_ForTeacher_ReturnsExpectedDisciplines()
  126. {
  127. // Arrange: Create a mock object for Schekaleva31pContext
  128. var dbContext = new Schekaleva31pContext();
  129. // Create an instance of DisciplineViewModel with the mock context
  130. var disciplineViewModel = new DisciplineViewModel(dbContext);
  131. // Set the SelectedOption to "Teacher" (Ïðåïîäàâàòåëü in Russian)
  132. disciplineViewModel.SelectedOption = "Ïðåïîäàâàòåëü";
  133. // Define the expected count of disciplines
  134. var expectedDisciplinesCount = 0;
  135. // Set the OptionValue to a specific teacher's name
  136. disciplineViewModel.OptionValue = "John Doe";
  137. // Act: Call the NewDisciplines method to populate the Disciplines property
  138. disciplineViewModel.NewDisciplines();
  139. // Assert: Verify that the count of disciplines matches the expected count
  140. Assert.AreEqual(expectedDisciplinesCount, disciplineViewModel.Disciplines?.Count);
  141. }
  142. [TestMethod]
  143. public void UserProfileViewModel_GetDisciplines_ReturnsExpectedDisciplines()
  144. {
  145. // Arrange: Create a mock object for Schekaleva31pContext
  146. var dbContext = new Schekaleva31pContext();
  147. // Define the email of the user
  148. var userEmail = "alex.ivanov@example.com";
  149. // Define the expected count of disciplines
  150. var expectedDisciplinesCount = 4;
  151. // Act: Create an instance of UserProfileViewModel with the mock context and user email
  152. var userProfileViewModel = new UserProfileViewModel(dbContext, userEmail);
  153. // Get the list of disciplines
  154. var disciplines = userProfileViewModel.Disciplines;
  155. // Assert: Verify that the count of disciplines matches the expected count
  156. Assert.AreEqual(expectedDisciplinesCount, disciplines.Count);
  157. }
  158. [TestMethod]
  159. public void UserProfileViewModel_Initialization_UserIsNotNull()
  160. {
  161. // Arrange: Create a mock object for Schekaleva31pContext
  162. var dbContext = new Schekaleva31pContext();
  163. // Define the email of the user
  164. var userEmail = "alex.ivanov@example.com";
  165. // Act: Create an instance of UserProfileViewModel with the mock context and user email
  166. var userProfileViewModel = new UserProfileViewModel(dbContext, userEmail);
  167. // Assert: Verify that the User property is not null
  168. Assert.IsNotNull(userProfileViewModel.User);
  169. }
  170. }
  171. }