Exemplo n.º 1
0
        public void GetAuthorByName_WithTest_ReturnAuthor()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedAuthors = MockDbSetHelper.CreateDbSetMock(GetAuthors());
                mock.Mock <IApplicationDbContext>().Setup(m => m.Authors).Returns(mockedAuthors.Object);

                var service = mock.Create <AuthorService>();

                var result = service.GetAuthorByName("test");

                Assert.Equal(authorId1, result.Id);
            }
        }
Exemplo n.º 2
0
        public void GetPost_InvalidId_ThrowsError()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedPosts = MockDbSetHelper.CreateDbSetMock(GetPosts());
                mock.Mock <IApplicationDbContext>().Setup(m => m.Posts).Returns(mockedPosts.Object);

                var service = mock.Create <PostService>();

                Action act = () => service.GetPost(Guid.NewGuid());

                Assert.Throws <Exception>(act);
            }
        }
Exemplo n.º 3
0
        public void GetAuthors_ReturnAll_Success()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedAuthors = MockDbSetHelper.CreateDbSetMock(GetAuthors());
                mock.Mock <IApplicationDbContext>().Setup(m => m.Authors).Returns(mockedAuthors.Object);

                var service = mock.Create <AuthorService>();

                var result = service.GetAuthors();

                Assert.Equal(2, result.Count());
            }
        }
Exemplo n.º 4
0
        public void GetPosts_ReturnAll_ReturnsAllPosts()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedPosts = MockDbSetHelper.CreateDbSetMock(GetPosts());
                mock.Mock <IApplicationDbContext>().Setup(m => m.Posts).Returns(mockedPosts.Object);

                var service = mock.Create <PostService>();

                var result = service.GetPosts();

                Assert.Equal(2, result.Count());
            }
        }
Exemplo n.º 5
0
        public void GetPost_WithPostId1_ReturnsPostDto()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedPosts = MockDbSetHelper.CreateDbSetMock(GetPosts());
                mock.Mock <IApplicationDbContext>().Setup(m => m.Posts).Returns(mockedPosts.Object);

                var service = mock.Create <PostService>();

                var result = service.GetPost(postId1);

                Assert.Equal("test", result.Title);
            }
        }
Exemplo n.º 6
0
        public void GetComment_WithValidId_ReturnsComment()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedComments = MockDbSetHelper.CreateDbSetMock(GetComments());
                mock.Mock <IApplicationDbContext>().Setup(m => m.Comments).Returns(mockedComments.Object);

                var service = mock.Create <CommentService>();

                var result = service.GetComment(commentId1);

                Assert.Equal("test", result.Content);
            }
        }
Exemplo n.º 7
0
        public void DeleteComment_WithInvalidId_ThrowsError()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedComments  = MockDbSetHelper.CreateDbSetMock(GetComments());
                var mockedDbContext = mock.Mock <IApplicationDbContext>();
                mockedDbContext.Setup(m => m.Comments).Returns(mockedComments.Object);

                var service = mock.Create <CommentService>();

                Action act = () => service.DeleteComment(Guid.NewGuid());

                Assert.Throws <Exception>(act);
            }
        }
Exemplo n.º 8
0
        public void DeleteComment_WithValidId_DeletesComment()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedComments  = MockDbSetHelper.CreateDbSetMock(GetComments());
                var mockedDbContext = mock.Mock <IApplicationDbContext>();
                mockedDbContext.Setup(m => m.Comments).Returns(mockedComments.Object);

                var service = mock.Create <CommentService>();

                service.DeleteComment(commentId1);

                mockedDbContext.Verify(m => m.SaveChanges(), Times.Once);
            }
        }
Exemplo n.º 9
0
        public void DeletePost_WithPostId1_DeletesPost()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedPosts     = MockDbSetHelper.CreateDbSetMock(GetPosts());
                var mockedDbContext = mock.Mock <IApplicationDbContext>();

                mockedDbContext.Setup(m => m.Posts).Returns(mockedPosts.Object);

                var service = mock.Create <PostService>();

                service.DeletePost(postId1);

                mockedDbContext.Verify(m => m.SaveChanges(), Times.Once);
            }
        }
Exemplo n.º 10
0
        public void GetCommentByPostId_WithValidId_ReturnsComment()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedComments = MockDbSetHelper.CreateDbSetMock(GetComments());
                var mockedPosts    = MockDbSetHelper.CreateDbSetMock(GetPosts());
                mock.Mock <IApplicationDbContext>().Setup(m => m.Comments).Returns(mockedComments.Object);
                mock.Mock <IApplicationDbContext>().Setup(m => m.Posts).Returns(mockedPosts.Object);

                var service = mock.Create <CommentService>();

                var result = service.GetCommentsByPostId(postId1);

                Assert.Single(result);
            }
        }
Exemplo n.º 11
0
        public void Setup()
        {
            var options = new DbContextOptionsBuilder <DataDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;

            _context  = new DataDbContext(options);
            _accounts = new List <AccountEntity> {
                new AccountEntity {
                    IBAN = "mock", Balance = 1000
                }
            };
            _context.Accounts = MockDbSetHelper.CreateDbSetMock(_accounts).Object;
            var config = new MapperConfiguration(cfg => { cfg.AddProfile(new AutoMapping()); });

            _target = new AccountRepository(_context, new Mapper(config));
        }
Exemplo n.º 12
0
        public void CreatePost_WithPostDto_ReturnsGuid()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedPosts = MockDbSetHelper.CreateDbSetMock(GetPosts());
                mock.Mock <IApplicationDbContext>().Setup(m => m.Posts).Returns(mockedPosts.Object);

                var service = mock.Create <PostService>();
                var id      = Guid.NewGuid();

                var result = service.CreatePost(new Blog.Dto.PostDto()
                {
                    Id = id
                });

                Assert.Equal(id, result);
            }
        }
Exemplo n.º 13
0
        public void UpdateComment_WithCommentDto_ReturnsCommentDto()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedComments = MockDbSetHelper.CreateDbSetMock(GetComments());
                mock.Mock <IApplicationDbContext>().Setup(m => m.Comments).Returns(mockedComments.Object);

                var service = mock.Create <CommentService>();

                var result = service.UpdateComment(new Blog.Dto.CommentDto()
                {
                    Id      = commentId1,
                    Content = "updated"
                });

                Assert.Equal("updated", result.Content);
            }
        }
Exemplo n.º 14
0
        public void CreateAuthor_WithAuthorObject_ReturnsGuid()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedAuthors = MockDbSetHelper.CreateDbSetMock(GetAuthors());
                mock.Mock <IApplicationDbContext>().Setup(m => m.Authors).Returns(mockedAuthors.Object);

                var service        = mock.Create <AuthorService>();
                var id             = Guid.NewGuid();
                var authorToCreate = new AuthorDto()
                {
                    Id = id
                };

                var result = service.CreateAuthor(authorToCreate);

                Assert.Equal(id, result);
            }
        }
Exemplo n.º 15
0
        public void UpdatePost_WithDifferentTitle_UpdatesTitle()
        {
            using (var mock = AutoMock.GetLoose())
            {
                var mockedPosts     = MockDbSetHelper.CreateDbSetMock(GetPosts());
                var mockedDbContext = mock.Mock <IApplicationDbContext>();

                mockedDbContext.Setup(m => m.Posts).Returns(mockedPosts.Object);

                var service = mock.Create <PostService>();

                var result = service.UpdatePost(new Blog.Dto.PostDto()
                {
                    Id    = postId1,
                    Title = "test123"
                });

                Assert.Equal("test123", result.Title);
            }
        }