public void TestGetAllWithLambda() { var posts = Enumerable.Range(0, 5).Select(x => new Post() { Url = string.Format("www.someurl{0}", x) }).ToList(); for (int i = 0; i < posts.Count; i++) { posts[i].PostComments.Add(new PostComment() { Comment = string.Format("some test comment {0}", i) }); } var queryablePosts = posts.AsQueryable(); var dbsetMock = CreateMockSet(queryablePosts); var mockContext = new Mock <SachaContextTestDouble>(); mockContext.Setup(c => c.Posts).Returns(dbsetMock.Object); var service = new SomeService(mockContext.Object); Func <Post, bool> func = (x) => x.Url == "www.someurl1"; Expression <Func <Post, bool> > filter = post => func(post); var retrievedPosts = service.GetAll(filter); CollectionAssert.AreEqual(posts.Where(func).ToList(), retrievedPosts.ToList()); }
public void TestGetAllWithLambda() { Mock <IUnitOfWork> uowMock = new Mock <IUnitOfWork>(); Mock <IRepository <Post> > repoMock = new Mock <IRepository <Post> >(); var posts = Enumerable.Range(0, 5) .Select(x => new Post() { Url = string.Format("www.someurl{0}", x) }).ToList(); for (int i = 0; i < posts.Count; i++) { posts[i].PostComments.Add(new PostComment() { Comment = string.Format("some test comment {0}", i) }); } repoMock.Setup(moq => moq.GetAll(It.IsAny <Expression <Func <Post, bool> > >())) .Returns((Expression <Func <Post, bool> > predicate) => posts.Where(predicate.Compile()).AsQueryable()); SomeService service = new SomeService(uowMock.Object, repoMock.Object); Func <Post, bool> func = (x) => x.Url == "www.someurl1"; Expression <Func <Post, bool> > filter = post => func(post); var retrievedPosts = service.GetAll(filter); CollectionAssert.AreEqual(posts.Where(func), retrievedPosts); }
public void TestGetAll() { Mock <IUnitOfWork> uowMock = new Mock <IUnitOfWork>(); Mock <IRepository <Post> > repoMock = new Mock <IRepository <Post> >(); var posts = Enumerable.Range(0, 5) .Select(x => new Post() { Url = string.Format("www.someurl{0}", x) }).ToList(); repoMock.Setup(x => x.GetAll()).Returns(posts.AsQueryable()); SomeService service = new SomeService(uowMock.Object, repoMock.Object); var retrievedPosts = service.GetAll(); repoMock.Verify(m => m.GetAll(), Times.Once()); CollectionAssert.AreEqual(posts, retrievedPosts); }
public void TestGetAll() { var posts = Enumerable.Range(0, 5).Select( x => new Post() { Url = string.Format("www.someurl{0}", x) }).AsQueryable(); var dbsetMock = CreateMockSet(posts); var mockContext = new Mock <SachaContextTestDouble>(); mockContext.Setup(c => c.Posts).Returns(dbsetMock.Object); var service = new SomeService(mockContext.Object); var retrievedPosts = service.GetAll().ToList(); var postsList = posts.ToList(); Assert.AreEqual(posts.Count(), retrievedPosts.Count()); Assert.AreEqual(postsList[0].Url, retrievedPosts[0].Url); Assert.AreEqual(postsList[4].Url, retrievedPosts[4].Url); }