public void ShouldPassCorrectly_WhenFilterParameterIsValid() { var mockDbSet = new Mock <DbSet <FakeEmployee> >(); var mockDbContext = new Mock <ISalaryCalculatorDbContext>(); mockDbContext.Setup(mock => mock.Set <FakeEmployee>()).Returns(mockDbSet.Object); var repo = new SalaryCalculatorRepository <FakeEmployee>(mockDbContext.Object); var fakeData = new List <FakeEmployee>() { new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, } .AsQueryable(); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.Provider).Returns(fakeData.Provider); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.Expression).Returns(fakeData.Expression); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.ElementType).Returns(fakeData.ElementType); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.GetEnumerator()).Returns(fakeData.GetEnumerator()); Expression <Func <FakeEmployee, bool> > filter = (FakeEmployee empl) => empl.FirstName.Equals("Alexander"); var actualResult = repo.GetAll(filter); Assert.That(actualResult.Count, Is.EqualTo(0)); }
public void GetAll_ShouldReturnAllData_WhenIsCalled() { var mockDbSet = new Mock <DbSet <FakeEmployee> >(); var mockDbContext = new Mock <ISalaryCalculatorDbContext>(); mockDbContext.Setup(mock => mock.Set <FakeEmployee>()).Returns(mockDbSet.Object); var repo = new SalaryCalculatorRepository <FakeEmployee>(mockDbContext.Object); // Setup Data var fakeModel = new Mock <FakeEmployee>(); var fakeData = new List <FakeEmployee>() { fakeModel.Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object } .AsQueryable(); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.Provider).Returns(fakeData.Provider); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.Expression).Returns(fakeData.Expression); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.ElementType).Returns(fakeData.ElementType); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.GetEnumerator()).Returns(fakeData.GetEnumerator()); var employees = repo.GetAll(); Assert.AreEqual(fakeData.Count(), employees.Count()); }
public void ShouldThrowArgumentNullException_WhenFilterParameterIsNull() { var mockDbSet = new Mock <DbSet <FakeEmployee> >(); var mockDbContext = new Mock <ISalaryCalculatorDbContext>(); mockDbContext.Setup(mock => mock.Set <FakeEmployee>()).Returns(mockDbSet.Object); var repo = new SalaryCalculatorRepository <FakeEmployee>(mockDbContext.Object); var fakeData = new List <FakeEmployee>() { new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, } .AsQueryable(); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.Provider).Returns(fakeData.Provider); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.Expression).Returns(fakeData.Expression); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.ElementType).Returns(fakeData.ElementType); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.GetEnumerator()).Returns(fakeData.GetEnumerator()); Expression <Func <FakeEmployee, bool> > filter = null; Assert.That( () => repo.GetAll(filter), Throws.InstanceOf <ArgumentNullException>().With.Message.Contains("Filter expression")); }
public void ShouldReturnCorrectCountOfItem_WhenItemIsFound() { var mockDbSet = new Mock <DbSet <FakeEmployee> >(); var mockDbContext = new Mock <ISalaryCalculatorDbContext>(); mockDbContext.Setup(mock => mock.Set <FakeEmployee>()).Returns(mockDbSet.Object); var repo = new SalaryCalculatorRepository <FakeEmployee>(mockDbContext.Object); // Setup Data var fakeModel = new Mock <FakeEmployee>(); fakeModel.SetupGet(model => model.Id).Returns(1); var fakeData = new List <FakeEmployee>() { fakeModel.Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object, new Mock <FakeEmployee>().Object } .AsQueryable(); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.Provider).Returns(fakeData.Provider); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.Expression).Returns(fakeData.Expression); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.ElementType).Returns(fakeData.ElementType); mockDbSet.As <IQueryable <FakeEmployee> >().Setup(m => m.GetEnumerator()).Returns(fakeData.GetEnumerator()); Expression <Func <FakeEmployee, bool> > filter = (FakeEmployee model) => model.Id == 1; var actualReturnedCollection = repo.GetAll(filter); var expectedCollection = new List <FakeEmployee>() { fakeModel.Object }; Assert.That(actualReturnedCollection.Count(), Is.Not.Null.And.EquivalentTo(expectedCollection)); }