Ejemplo n.º 1
0
        public async Task Index_GetAll()
        {
            // Arrange
            Moq.Mock <IBloggingRepository> mock       = MockBloggingRepository();
            IBloggingRepository            repository = mock.Object;
            var logger = LoggerFactory.CreateLogger <BlogsController>();

            using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider))
            {
                // Act
                var result = await controller.Index();

                // Assert
                Assert.NotNull(result);

                Assert.IsType <ViewResult>(result);
                Assert.IsAssignableFrom <IEnumerable <DecoratedBlog> >(((ViewResult)result).Model);
                Assert.NotEmpty(((IEnumerable <DecoratedBlog>)((ViewResult)result).Model));
                Assert.Equal(1, ((IEnumerable <DecoratedBlog>)((ViewResult)result).Model).First().BlogId);
                Assert.Equal("http://www.some-url.com", ((IEnumerable <DecoratedBlog>)((ViewResult)result).Model).First().Url);
            }
        }
Ejemplo n.º 2
0
        public async Task InvalidEdit()
        {
            // Arrange
            Moq.Mock <IBloggingRepository> mock       = MockBloggingRepository();
            IBloggingRepository            repository = mock.Object;
            var logger = LoggerFactory.CreateLogger <BlogsController>();

            //1st add one object
            await repository.AddAsync(new Blog
            {
                BlogId = -99999,
                Post   = null,
                Url    = "http://www.999999.com",
            });

            await repository.SaveAsync();

            using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider))
            {
                var model = new DecoratedBlog
                {
                    BlogId      = -99999,
                    Post        = null,
                    Url         = "http://www.minus999999.com",
                    ProtectedID = "giberish"
                };

                // Act
                ((Controller)controller).ModelState.AddModelError(nameof(Blog.Url), "bad id");

                var result = await controller.BlogAction(model, true, BlogActionEnum.Edit);

                // Assert
                Assert.IsType <JsonResult>(result);
                Assert.IsType <string>(((JsonResult)result).Value);
                Assert.Equal("error", ((JsonResult)result).Value.ToString());
            }
        }
Ejemplo n.º 3
0
        public async Task Edit()
        {
            // Arrange
            Moq.Mock <IBloggingRepository> mock       = MockBloggingRepository();
            IBloggingRepository            repository = mock.Object;
            var logger = LoggerFactory.CreateLogger <BlogsController>();

            //1st add one object
            await repository.AddAsync(new Blog
            {
                BlogId = 2,
                Post   = null,
                Url    = "http://www.internet.com",
            });

            await repository.SaveAsync();

            using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider))
            {
                var model = new DecoratedBlog
                {
                    BlogId      = 2,
                    Post        = null,
                    Url         = "http://www.changed-internet.com",
                    ProtectedID = "giberish"
                };

                // Act
                var result = await controller.BlogAction(model, true, BlogActionEnum.Edit);

                // Assert
                Assert.IsType <JsonResult>(result);
                Assert.IsType <Blog>(((JsonResult)result).Value);
                Assert.Equal(2, ((Blog)((JsonResult)result).Value).BlogId);
                Assert.Contains("changed", ((Blog)((JsonResult)result).Value).Url);
            }
        }
Ejemplo n.º 4
0
        public async Task Create()
        {
            // Arrange
            Moq.Mock <IBloggingRepository> mock       = MockBloggingRepository();
            IBloggingRepository            repository = mock.Object;
            var logger = LoggerFactory.CreateLogger <BlogsController>();

            using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider))
            {
                // Act
                var result = controller.Create();

                // Assert
                Assert.NotNull(result);

                Assert.IsType <ViewResult>(result);
                Assert.Null(((ViewResult)result).Model);
            }

            using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider))
            {
                // Act
                var result = await controller.Create(new Blog
                {
                    BlogId = 2,
                    Post   = null,
                    Url    = "http://www.internet.com"
                });

                // Assert
                Assert.NotNull(result);

                Assert.IsType <RedirectResult>(result);
                Assert.Equal($"{Configuration["AppRootPath"]}{BlogsController.ASPX}", ((RedirectResult)result).Url);
            }
        }
        public async Task Delete()
        {
            // Arrange
            Moq.Mock <IBloggingRepository> mock       = MockBloggingRepository();
            IBloggingRepository            repository = mock.Object;
            var logger = LoggerFactory.CreateLogger <BlogsController>();

            //1st add one object
            await repository.AddAsync(new Blog
            {
                BlogId = 2,
                Post   = null,
                Url    = "http://www.internet.com",
            });

            await repository.SaveAsync();

            using (IBlogsController controller = new BlogsController(repository, logger, Configuration, DataProtectionProvider))
            {
                var model = new DecoratedBlog
                {
                    BlogId      = 2,
                    Post        = null,
                    Url         = "bad_url_but_no_matter",
                    ProtectedID = "giberish"
                };

                // Act
                var result = await controller.ItemAction(model, true, BlogActionEnum.Delete);

                // Assert
                Assert.IsType <JsonResult>(result);
                Assert.IsType <string>(((JsonResult)result).Value);
                Assert.Equal("deleted", ((JsonResult)result).Value.ToString());
            }
        }