Click here to Skip to main content
15,881,653 members
Articles / Desktop Programming / WPF

Building WPF Applications with Self-Tracking Entity Generator - Project Setup

Rate me:
Please Sign up or sign in to vote.
4.80/5 (11 votes)
20 Feb 2012CPOL10 min read 75.2K   4.8K   54  
This article describes the project setup of building a WPF sample application with Self-Tracking Entity Generator for WPF/Silverlight.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GalaSoft.MvvmLight.Command;
using Moq;
using SchoolSample.Common;
using SchoolSample.EntityModel;
using SchoolSample.ViewModel;


namespace Test.SchoolSample.ViewModel
{
    /// <summary>
    ///This is a test class for StudentPageViewModelTest and is intended
    ///to contain all StudentPageViewModelTest Unit Tests
    ///</summary>
    [TestClass]
    public class StudentPageViewModelTest
    {
        private TestContext testContextInstance;
        private Mock<ISchoolModel> schoolModelMock;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes

        //Use TestInitialize to run code before running each test
        [TestInitialize]
        public void MyTestInitialize()
        {
            schoolModelMock = new Mock<ISchoolModel>();
            var studentList = new List<Student>
                                  {
                                      new Student
                                          {
                                              PersonId = 1,
                                              Name = "Test Student One",
                                              Enrollments = new TrackableCollection<Enrollment>
                                                                {
                                                                    new Enrollment
                                                                        {
                                                                            EnrollmentId = 1,
                                                                            CourseId = 1,
                                                                            StudentId = 1
                                                                        },
                                                                    new Enrollment
                                                                        {
                                                                            EnrollmentId = 2,
                                                                            CourseId = 2,
                                                                            StudentId = 1
                                                                        },
                                                                    new Enrollment
                                                                        {
                                                                            EnrollmentId = 3,
                                                                            CourseId = 3,
                                                                            StudentId = 1
                                                                        },
                                                                }
                                          },
                                      new Student
                                          {
                                              PersonId = 2,
                                              Name = "Test Student Two",
                                              Enrollments = new TrackableCollection<Enrollment>
                                                                {
                                                                    new Enrollment
                                                                        {
                                                                            EnrollmentId = 4,
                                                                            CourseId = 1,
                                                                            StudentId = 2
                                                                        },
                                                                    new Enrollment
                                                                        {
                                                                            EnrollmentId = 5,
                                                                            CourseId = 2,
                                                                            StudentId = 2
                                                                        },
                                                                    new Enrollment
                                                                        {
                                                                            EnrollmentId = 6,
                                                                            CourseId = 3,
                                                                            StudentId = 2
                                                                        },
                                                                }
                                          },
                                      new Student
                                          {
                                              PersonId = 3,
                                              Name = "Test Student Three",
                                              Enrollments = new TrackableCollection<Enrollment>
                                                                {
                                                                    new Enrollment
                                                                        {
                                                                            EnrollmentId = 7,
                                                                            CourseId = 1,
                                                                            StudentId = 3
                                                                        },
                                                                    new Enrollment
                                                                        {
                                                                            EnrollmentId = 8,
                                                                            CourseId = 2,
                                                                            StudentId = 3
                                                                        },
                                                                    new Enrollment
                                                                        {
                                                                            EnrollmentId = 9,
                                                                            CourseId = 3,
                                                                            StudentId = 3
                                                                        },
                                                                }
                                          }
                                  };
            schoolModelMock.Setup(n => n.GetStudentsAsync("Enrollments.Course", "StudentPage"))
                .Raises(n => n.GetStudentsCompleted += null,
                        new ResultsArgs<Student>(studentList, null, false, "StudentPage"));
            schoolModelMock.SetupAllProperties();
        }

        //Use TestCleanup to run code after each test has run
        [TestCleanup]
        public void MyTestCleanup()
        {
            schoolModelMock = null;
        }

        #endregion


        /// <summary>
        ///A test for StudentPageViewModel Constructor
        ///</summary>
        [TestMethod]
        public void StudentPageViewModelConstructorTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            schoolModelMock.Verify(n => n.GetStudentsAsync("Enrollments.Course", "StudentPage"), Times.Once());
            Assert.IsFalse(target.StudentFormInEdit);
        }

        /// <summary>
        ///A test for AllStudentsSourceView_CurrentChanged
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void AllStudentsSourceView_CurrentChangedTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.CurrentStudent = null;
            target.AllStudentsSourceView_CurrentChanged(null, null);
            Assert.IsNotNull(target.CurrentStudent);
        }

        /// <summary>
        ///A test for OnAddStudentCommand
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void OnAddStudentCommandTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.OnAddStudentCommand();
            Assert.IsTrue(target.CurrentStudent.PersonId < 0);
            Assert.IsTrue(target.StudentFormInEdit);
        }

        /// <summary>
        ///A test for OnCancelEditStudentCommand
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void OnCancelEditStudentCommandTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.StudentFormInEdit = true;
            target.OnCancelEditStudentCommand();
            Assert.IsFalse(target.StudentFormInEdit);
        }

        /// <summary>
        ///A test for OnEditCommitStudentCommand
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void OnEditCommitStudentCommandTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.StudentFormInEdit = true;
            target.OnEditCommitStudentCommand();
            Assert.IsFalse(target.StudentFormInEdit);
        }

        /// <summary>
        ///A test for OnPageLoadedCommand
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void OnPageLoadedCommandTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            schoolModelMock.Object.StudentsList = null;
            schoolModelMock.Object.CurrentStudent = null;
            target.OnPageLoadedCommand();
            Assert.IsNotNull(schoolModelMock.Object.StudentsList);
            Assert.IsNotNull(schoolModelMock.Object.CurrentStudent);
        }

        /// <summary>
        ///A test for OnPageUnLoadedCommand
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void OnPageUnLoadedCommandTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.OnPageUnLoadedCommand();
            Assert.IsNull(schoolModelMock.Object.StudentsList);
            Assert.IsNull(schoolModelMock.Object.CurrentStudent);
        }

        /// <summary>
        ///A test for OnRefreshAllCommand
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void OnRefreshAllCommandTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.OnRefreshAllCommand();
            schoolModelMock.Verify(n => n.GetStudentsAsync("Enrollments.Course", "StudentPage"), Times.Exactly(2));
        }

        /// <summary>
        ///A test for OnRefreshStudentCommand
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void OnRefreshStudentCommandTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.OnRefreshStudentCommand();
            schoolModelMock.Verify(n => n.GetStudentByIdAsync(It.IsAny<int>()), Times.Once());
        }

        /// <summary>
        ///A test for OnSubmitAllChangeCommand
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void OnSubmitAllChangeCommandTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.OnSubmitAllChangeCommand();
            schoolModelMock.Verify(n => n.SaveStudentChangesAsync(true), Times.Once());
        }

        /// <summary>
        ///A test for OnSubmitStudentChangeCommand
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void OnSubmitStudentChangeCommandTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.OnSubmitStudentChangeCommand();
            schoolModelMock.Verify(n => n.SaveStudentChangesAsync(false), Times.Once());
        }

        /// <summary>
        ///A test for StudentFormBeginEdit
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void StudentFormBeginEditTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.StudentFormInEdit = false;
            target.StudentFormBeginEdit();
            Assert.IsTrue(target.StudentFormInEdit);
        }

        /// <summary>
        ///A test for StudentFormCancelEdit
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void StudentFormCancelEditTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.StudentFormInEdit = true;
            target.StudentFormCancelEdit();
            Assert.IsFalse(target.StudentFormInEdit);
        }

        /// <summary>
        ///A test for StudentFormEndEdit
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void StudentFormEndEditTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.StudentFormInEdit = true;
            target.StudentFormEndEdit();
            Assert.IsFalse(target.StudentFormInEdit);
        }

        /// <summary>
        ///A test for StudentPageViewModel_PropertyChanged
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void StudentPageViewModel_PropertyChangedTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.StudentFormInEdit = false;
            target.StudentListIsEnabled = false;
            PropertyChangedEventArgs e = new PropertyChangedEventArgs("StudentFormInEdit");
            target.StudentPageViewModel_PropertyChanged(null, e);
            Assert.IsTrue(target.StudentListIsEnabled);
        }

        /// <summary>
        ///A test for _schoolModel_GetStudentByIdCompleted
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void _schoolModel_GetStudentByIdCompletedTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            ResultArgs<Student> e = new ResultArgs<Student>(
                new Student
                    {
                        PersonId = 1,
                        Name = "Test Student One"
                    }, null, false, null);
            target._schoolModel_GetStudentByIdCompleted(null, e);
            Assert.IsTrue(target.CurrentStudent.Enrollments.Count == 0);
        }

        /// <summary>
        ///A test for _schoolModel_GetStudentsCompleted
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void _schoolModel_GetStudentsCompletedTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            ResultsArgs<Student> e = new ResultsArgs<Student>(new List<Student>(), null, false, "StudentPage");
            target._schoolModel_GetStudentsCompleted(null, e);
            Assert.IsTrue(target.AllStudents.Count == 0);
            Assert.IsNull(target.CurrentStudent);
        }

        /// <summary>
        ///A test for _schoolModel_SaveStudentChangesCompleted
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void _schoolModel_SaveStudentChangesCompletedTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            ResultArgs<string> e = new ResultArgs<string>(string.Empty, null, false, null);
            target._schoolModel_SaveStudentChangesCompleted(null, e);
            schoolModelMock.Verify(n => n.GetStudentsAsync("Enrollments.Course", "StudentPage"), Times.Exactly(2));
        }

        /// <summary>
        ///A test for AllStudents
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void AllStudentsTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            ObservableCollection<Student> expected = new ObservableCollection<Student>();
            target.AllStudents = expected;
            ObservableCollection<Student> actual = target.AllStudents;
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for AllStudentsSource
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void AllStudentsSourceTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            CollectionViewSource expected = new CollectionViewSource();
            target.AllStudentsSource = expected;
            CollectionViewSource actual = target.AllStudentsSource;
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for CurrentStudent
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void CurrentStudentTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            Student expected = new Student();
            target.CurrentStudent = expected;
            Student actual = target.CurrentStudent;
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for CurrentStudentHasErrors
        ///</summary>
        [TestMethod]
        public void CurrentStudentHasErrorsTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            target.CurrentStudentHasErrors = true;
            bool actual = target.CurrentStudentHasErrors;
            Assert.IsTrue(actual);
        }

        /// <summary>
        ///A test for StudentFormInEdit
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void StudentFormInEditTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.StudentFormInEdit = true;
            bool actual = target.StudentFormInEdit;
            Assert.IsTrue(actual);
        }

        /// <summary>
        ///A test for StudentListIsEnabled
        ///</summary>
        [TestMethod]
        [DeploymentItem("SchoolSample.ViewModel.dll")]
        public void StudentListIsEnabledTest()
        {
            StudentPageViewModel_Accessor target = new StudentPageViewModel_Accessor(schoolModelMock.Object);
            target.StudentListIsEnabled = true;
            bool actual = target.StudentListIsEnabled;
            Assert.IsTrue(actual);
        }

        /// <summary>
        ///A test for AddStudentCommand
        ///</summary>
        [TestMethod]
        public void AddStudentCommandTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            RelayCommand actual = target.AddStudentCommand;
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for CancelAllChangeCommand
        ///</summary>
        [TestMethod]
        public void CancelAllChangeCommandTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            RelayCommand actual = target.CancelAllChangeCommand;
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for CancelEditStudentCommand
        ///</summary>
        [TestMethod]
        public void CancelEditStudentCommandTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            RelayCommand actual = target.CancelEditStudentCommand;
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for CancelStudentChangeCommand
        ///</summary>
        [TestMethod]
        public void CancelStudentChangeCommandTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            RelayCommand actual = target.CancelStudentChangeCommand;
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for DeleteStudentCommand
        ///</summary>
        [TestMethod]
        public void DeleteStudentCommandTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            RelayCommand actual = target.DeleteStudentCommand;
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for EditCommitStudentCommand
        ///</summary>
        [TestMethod]
        public void EditCommitStudentCommandTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            RelayCommand actual = target.EditCommitStudentCommand;
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for PageLoadedCommand
        ///</summary>
        [TestMethod]
        public void PageLoadedCommandTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            RelayCommand actual = target.PageLoadedCommand;
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for PageUnLoadedCommand
        ///</summary>
        [TestMethod]
        public void PageUnLoadedCommandTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            RelayCommand actual = target.PageUnLoadedCommand;
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for RefreshAllCommand
        ///</summary>
        [TestMethod]
        public void RefreshAllCommandTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            RelayCommand actual = target.RefreshAllCommand;
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for RefreshStudentCommand
        ///</summary>
        [TestMethod]
        public void RefreshStudentCommandTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            RelayCommand actual = target.RefreshStudentCommand;
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for SubmitAllChangeCommand
        ///</summary>
        [TestMethod]
        public void SubmitAllChangeCommandTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            RelayCommand actual = target.SubmitAllChangeCommand;
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///A test for SubmitStudentChangeCommand
        ///</summary>
        [TestMethod]
        public void SubmitStudentChangeCommandTest()
        {
            StudentPageViewModel target = new StudentPageViewModel(schoolModelMock.Object);
            RelayCommand actual = target.SubmitStudentChangeCommand;
            Assert.IsNotNull(actual);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

Comments and Discussions