Ejemplo n.º 1
0
        public void AddMarkToDatabase()
        {
            // Arrange
            var classes   = new List <StudentClass>();
            var queryable = classes.AsQueryable();

            var mockDbSet = new Mock <IDbSet <StudentClass> >();

            mockDbSet.As <IQueryable <StudentClass> >().Setup(m => m.Provider).Returns(queryable.Provider);
            mockDbSet.As <IQueryable <StudentClass> >().Setup(m => m.Expression).Returns(queryable.Expression);
            mockDbSet.As <IQueryable <StudentClass> >().Setup(m => m.ElementType).Returns(queryable.ElementType);
            mockDbSet.As <IQueryable <StudentClass> >().Setup(m => m.GetEnumerator()).Returns(() => queryable.GetEnumerator());
            mockDbSet.Setup(d => d.Add(It.IsAny <StudentClass>())).Callback <StudentClass>((cl) => classes.Add(cl));

            var mockDbContext = new Mock <IDatabaseContext>();

            mockDbContext.Setup(c => c.StudentClasses).Returns(mockDbSet.Object);

            var studentClassService = new StudentClassService(mockDbContext.Object);
            var studentClass        = new StudentClass("1A");

            // Act
            studentClassService.Add(studentClass);

            // Assert
            Assert.IsTrue(classes.Count() == 1);
            Assert.IsTrue(classes.Contains(studentClass));
        }
Ejemplo n.º 2
0
        public void ReturnAllClasses()
        {
            // Arrange
            var classes = new List <StudentClass>()
            {
                new StudentClass("1A"),
                new StudentClass("2A"),
                new StudentClass("3A")
            }.AsQueryable();


            var mockedDbSet = new Mock <IDbSet <StudentClass> >();

            mockedDbSet.As <IQueryable <StudentClass> >().Setup(m => m.Provider).Returns(classes.Provider);
            mockedDbSet.As <IQueryable <StudentClass> >().Setup(m => m.Expression).Returns(classes.Expression);
            mockedDbSet.As <IQueryable <StudentClass> >().Setup(m => m.ElementType).Returns(classes.ElementType);
            mockedDbSet.As <IQueryable <StudentClass> >().Setup(m => m.GetEnumerator()).Returns(() => classes.GetEnumerator());

            var mockedDbContext = new Mock <IDatabaseContext>();

            mockedDbContext.Setup(c => c.StudentClasses).Returns(mockedDbSet.Object);

            var studentClassService = new StudentClassService(mockedDbContext.Object);

            // Act
            var allClasses = studentClassService.GetAll().ToList();

            // Assert
            CollectionAssert.AreEqual(classes, allClasses);
        }
Ejemplo n.º 3
0
        public void ReturnNull_IfIdNotMatched()
        {
            // Arrange
            var classes   = this.GetClasses();
            var queryable = classes.AsQueryable();

            var mockedDbSet = new Mock <IDbSet <StudentClass> >();

            mockedDbSet.As <IQueryable <StudentClass> >().Setup(m => m.Provider).Returns(queryable.Provider);
            mockedDbSet.As <IQueryable <StudentClass> >().Setup(m => m.Expression).Returns(queryable.Expression);
            mockedDbSet.As <IQueryable <StudentClass> >().Setup(m => m.ElementType).Returns(queryable.ElementType);
            mockedDbSet.As <IQueryable <StudentClass> >().Setup(m => m.GetEnumerator()).Returns(() => queryable.GetEnumerator());

            var mockedDbContext = new Mock <IDatabaseContext>();

            mockedDbContext.Setup(c => c.StudentClasses).Returns(mockedDbSet.Object);

            var studentClassService = new StudentClassService(mockedDbContext.Object);

            // Act
            var id = Guid.NewGuid().ToString();
            var cl = studentClassService.FindById(id);

            // Assert
            Assert.IsNull(cl);
        }
        public void ShouldReturnClass_IfTeacherIdMatched()
        {
            // Arrange
            var teacherId = Guid.NewGuid().ToString();
            var classes   = new List <StudentClass>()
            {
                new StudentClass("1A"),
                new StudentClass("2A")
                {
                    FormMasterId = teacherId
                },
                new StudentClass("3A")
            };
            var queryable = classes.AsQueryable();

            var mockedDbSet = new Mock <IDbSet <StudentClass> >();

            mockedDbSet.As <IQueryable <StudentClass> >().Setup(m => m.Provider).Returns(queryable.Provider);
            mockedDbSet.As <IQueryable <StudentClass> >().Setup(m => m.Expression).Returns(queryable.Expression);
            mockedDbSet.As <IQueryable <StudentClass> >().Setup(m => m.ElementType).Returns(queryable.ElementType);
            mockedDbSet.As <IQueryable <StudentClass> >().Setup(m => m.GetEnumerator()).Returns(() => queryable.GetEnumerator());

            var mockedDbContext = new Mock <IDatabaseContext>();

            mockedDbContext.Setup(c => c.StudentClasses).Returns(mockedDbSet.Object);

            var studentClassService = new StudentClassService(mockedDbContext.Object);

            // Act
            var cl = studentClassService.FindByTeacherId(teacherId);

            // Assert
            Assert.AreEqual(classes[1], cl);
            Assert.AreEqual(classes[1].FormMasterId, teacherId);
        }
Ejemplo n.º 5
0
        private DataSet ds = null;     //保存全部查询结果的数据集
        public FrmScoreQuery()
        {
            InitializeComponent();
            this.dgvScoreList.AutoGenerateColumns = false;
            StudentClassService _studentClassService = new StudentClassService();

            this.cboClass.DataSource    = _studentClassService.GetAllClass();
            this.cboClass.DisplayMember = "ClassName";
            this.cboClass.ValueMember   = "ClassId";
            this.cboClass.SelectedIndex = -1;
        }
Ejemplo n.º 6
0
        public void CallDbContext_SaveMethod()
        {
            // Arrange
            var mockedDbContext = new Mock <IDatabaseContext>();

            mockedDbContext.Setup(c => c.Save()).Returns(1).Verifiable();

            var studentClassService = new StudentClassService(mockedDbContext.Object);

            // Act
            var result = studentClassService.Save();

            // Assert
            Assert.IsTrue(result == 1);
            mockedDbContext.Verify(c => c.Save(), Times.Once);
        }
Ejemplo n.º 7
0
        public FrmScoreQuery()
        {
            InitializeComponent();
            //班级下拉列表-获取全部的班级信息
            DataTable dt = new StudentClassService().GetAllClass().Tables[0];

            //绑定
            this.cboClass.DataSource    = dt;
            this.cboClass.ValueMember   = "ClassId";
            this.cboClass.DisplayMember = "ClassName";
            this.cboClass.SelectedIndex = -1;
            //显示全部查询成绩
            ds = objListService.GetAllScoreList();
            this.dgvScoreList.DataSource = ds.Tables[0];
            //调整显示风格
            new Common.DataGridViewStyle().DgvStyle3(this.dgvScoreList);
        }
        private DataSet ds = null;//保存全部查询结果的数据集
        public FrmScoreQuery()
        {
            InitializeComponent();
            //显示班级下拉框
            DataTable dt = new StudentClassService().GetAllClass().Tables[0];

            this.cboClass.DataSource    = dt;
            this.cboClass.ValueMember   = "ClassId";
            this.cboClass.DisplayMember = "ClassName";
            //显示全部考试成绩
            ds = objListService.GetAllScoreList();
            this.dgvScoreList.DataSource = ds.Tables[0];
            new Common.DataGridViewStyle().DgvStyle3(this.dgvScoreList);

            //禁止列排序,保证列标题居中
            foreach (DataGridViewColumn item in this.dgvScoreList.Columns)
            {
                item.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }