public void WhenGetTeamCalled_ThenReturnsFilteredCollection() { // Arrange var superPeople = new List <SuperPerson>() { new SuperPerson() { Id = 1, Allegiance = "A" }, new SuperPerson() { Id = 2, Allegiance = "B" }, new SuperPerson() { Id = 3, Allegiance = "A" }, }; var filteredSuperPeople = new List <SuperPerson>() { superPeople[0], superPeople[2] }; var mockEntitySet = new Mock <IEntitySet <SuperPerson> >(); var mockEnumerable = mockEntitySet.As <IEnumerable <SuperPerson> >(); mockEnumerable.Setup(x => x.GetEnumerator()).Returns(superPeople.GetEnumerator()); // Note: Moq example of an mocking a method specific to an interface (complexity => Reflector) var mockQueryable = mockEntitySet.As <IQueryable <SuperPerson> >(); mockQueryable.Setup(x => x.Provider).Returns(superPeople.AsQueryable().Provider); mockQueryable.Setup(x => x.Expression).Returns(superPeople.AsQueryable().Expression); 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.GetTeam("A"); // Assert var actualList = actual.ToList(); Assert.AreEqual(2, actualList.Count); Assert.AreSame(superPeople[0], actualList[0]); Assert.AreSame(superPeople[2], actualList[1]); }
public void WhenGetTeamCalledWithEmptyString_ThenThrows() { // Arrange var mockContext = new Mock <ISuperDatabaseContext>(); ISuperDatabaseContext context = mockContext.Object; SuperRepository target = new SuperRepository(context); // Act target.GetTeam(string.Empty); // Assert }