Beispiel #1
0
        public void WhenGetAllCalled_ThenReturnsCollection()
        {
            // Arrange
            var superPeople = new List <SuperPerson>()
            {
                new SuperPerson()
                {
                    Id = 1
                },
                new SuperPerson()
                {
                    Id = 2
                },
                new SuperPerson()
                {
                    Id = 3
                },
            };

            var mockEntitySet = new Mock <IEntitySet <SuperPerson> >();

            // Note: Moq example of an mocking a method specific to an interface

            var mockEnumerable = mockEntitySet.As <IEnumerable <SuperPerson> >();

            mockEnumerable.Setup(x => x.GetEnumerator()).Returns(superPeople.GetEnumerator());

            // Note: Moq example of an mocking the return of a get accessor
            var mockContext = new Mock <ISuperDatabaseContext>();

            mockContext.Setup(x => x.SuperPeople).Returns(mockEntitySet.Object);

            ISuperDatabaseContext context = mockContext.Object;
            SuperRepository       target  = new SuperRepository(context);

            // Act
            var actual = target.GetAll();

            // Assert
            var actualList = actual.ToList();

            Assert.AreEqual(superPeople.Count, actualList.Count);

            for (int i = 0; i < superPeople.Count; i++)
            {
                Assert.AreSame(superPeople[i], actualList[i]);
            }
        }