public void CreateBlog_saves_a_blog_via_context() { //var mockSet = new Mock<DbSet<Blog>>(); //var mockContext = new Mock<BloggingContext>(); //mockContext.Setup(m => m.Blogs).Returns(mockSet.Object); //var service = new BlogService(mockContext.Object); //service.AddBlog("ADO.NET Blog", "http://blogs.msdn.com/adonet"); //mockSet.Verify(m => m.Add(It.IsAny<Blog>()), Times.Once()); //mockContext.Verify(m => m.SaveChanges(), Times.Once()); var mockSet = MockRepository.GenerateStub<DbSet<Blog>>(); mockSet.Stub(x => x.Add(null)).IgnoreArguments().Repeat.Once(); var mockContext = MockRepository.GenerateStub<BloggingContext>(); // mockContext.Stub(x => x.Blogs).Return(mockSet); mockContext.Blogs = mockSet; mockContext.Stub(x => x.SaveChanges()).Return(1).Repeat.Once(); var repositorio = new BlogService(mockContext); repositorio.AddBlog("ADO.NET Blog", "http://blogs.msdn.com/adonet"); mockSet.VerifyAllExpectations(); mockContext.VerifyAllExpectations(); }
public void GetAllBlogs_orders_by_name() { var data = new List<Blog> { new Blog { Name = "BBB" }, new Blog { Name = "ZZZ" }, new Blog { Name = "AAA" }, }.AsQueryable(); var mockSet = MockRepository.GenerateMock<IDbSet<Blog>, IQueryable>(); mockSet.Stub(x => x.Provider).Return(data.Provider); mockSet.Stub(x => x.Expression).Return(data.Expression); mockSet.Stub(x => x.ElementType).Return(data.ElementType); mockSet.Stub(x => x.GetEnumerator()).Return(data.GetEnumerator()); var mockContext = MockRepository.GenerateStub<BloggingContext>(); //mockContext.Stub(x => x.Blogs).PropertyBehavior(); mockContext.Blogs = mockSet; var service = new BlogService(mockContext); var blogs = service.GetAllBlogs(); Assert.AreEqual(3, blogs.Count); Assert.AreEqual("AAA", blogs[0].Name); Assert.AreEqual("BBB", blogs[1].Name); Assert.AreEqual("ZZZ", blogs[2].Name); //var mockSet = new Mock<DbSet<Blog>>(); //mockSet.As<IQueryable<Blog>>().Setup(m => m.Provider).Returns(data.Provider); //mockSet.As<IQueryable<Blog>>().Setup(m => m.Expression).Returns(data.Expression); //mockSet.As<IQueryable<Blog>>().Setup(m => m.ElementType).Returns(data.ElementType); //mockSet.As<IQueryable<Blog>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); //var mockContext = new Mock<BloggingContext>(); //mockContext.Setup(c => c.Blogs).Returns(mockSet.Object); //var service = new BlogService(mockContext.Object); //var blogs = service.GetAllBlogs(); //Assert.AreEqual(3, blogs.Count); //Assert.AreEqual("AAA", blogs[0].Name); //Assert.AreEqual("BBB", blogs[1].Name); //Assert.AreEqual("ZZZ", blogs[2].Name); }