Beispiel #1
0
        public async Task Create_WithWrongImageFormat_ShouldThrowException()
        {
            var list = new List <News>();

            var mockRepo = new Mock <IDeletableEntityRepository <News> >();

            mockRepo.Setup(x => x.All()).Returns(list.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <News>())).Callback((News news) => list.Add(news));

            var service = new NewsService(mockRepo.Object);

            IFormFile image = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt");


            var newsCreateInput = new NewsCreateInputModel
            {
                UserId     = 6,
                Title      = "Arsenal",
                Content    = null,
                CategoryId = 6,
                Image      = image,
            };

            await Assert.ThrowsAsync <ArgumentException>(() => service.CreateAsync(newsCreateInput, "12", "/img/Arsenal_FC.png"));
        }
Beispiel #2
0
        public async Task Create_WithCorrectData_ShouldSuccessfullyCreate()
        {
            var list = new List <News>();

            var mockRepo = new Mock <IDeletableEntityRepository <News> >();

            mockRepo.Setup(x => x.All()).Returns(list.AsQueryable());
            mockRepo.Setup(x => x.AddAsync(It.IsAny <News>())).Callback((News news) => list.Add(news));

            var service = new NewsService(mockRepo.Object);

            IFormFile image = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.png");

            var newsCreateInput = new NewsCreateInputModel
            {
                UserId     = 6,
                Title      = "Arsenal",
                Content    = null,
                CategoryId = 6,
                Image      = image,
            };

            await service.CreateAsync(newsCreateInput, "12", "/img/Arsenal_FC.png");

            Assert.Single(list.Where(x => x.CreatedByUserId == "12"));
        }
        public async Task CreateAsyncShouldCreateOneObject()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "NewsTestSecond").Options;
            var dbContext  = new ApplicationDbContext(options);
            var repository = new EfDeletableEntityRepository <News>(dbContext);
            var service    = new NewsService(repository);
            await service.CreateAsync("test", "test", new byte[100]);

            Assert.Equal(1, service.GetCount());
        }
        public async Task GetAllReturnsAllNewsCorrectly()
        {
            var newsList = new List <News>();
            var newsRepo = new Mock <IDeletableEntityRepository <News> >();

            newsRepo.Setup(x => x.All()).Returns(newsList.AsQueryable());
            newsRepo.Setup(x => x.AddAsync(It.IsAny <News>()))
            .Callback((News r) => newsList.Add(r));

            var newsService = new NewsService(newsRepo.Object);

            await newsService.CreateAsync(test.Title, test.Content, test.PictureUrl, test.NewsType);

            await newsService.CreateAsync(test2.Title, test2.Content, test2.PictureUrl, test2.NewsType);

            var resultList = newsService.GetAll().ToList().OrderBy(x => x.CreatedOn).ToList();

            Assert.Equal(newsList[0].Title, resultList[0].Title);
            Assert.Equal(newsList[0].Id, resultList[0].Id);
            Assert.Equal(newsList[1].Title, resultList[1].Title);
            Assert.Equal(newsList[1].Id, resultList[1].Id);
        }
        public async Task GetLatestnewssGetsNoMoreThanFiveUniqueNews()
        {
            var store = new Mock <IUserStore <ApplicationUser> >();

            var userManager = new UserManager <ApplicationUser>(store.Object, null, null, null, null, null, null, null, null);

            var newsList = new List <News>();
            var newsRepo = new Mock <IDeletableEntityRepository <News> >();

            newsRepo.Setup(x => x.All()).Returns(newsList.AsQueryable());
            newsRepo.Setup(x => x.AddAsync(It.IsAny <News>()))
            .Callback((News r) => newsList.Add(r));

            var newsService = new NewsService(newsRepo.Object);

            await newsService.CreateAsync(test.Title, test.Content, test.PictureUrl, test.NewsType);

            await newsService.CreateAsync(test2.Title, test2.Content, test2.PictureUrl, test2.NewsType);

            var listResult = newsService.GetLatestNews();

            Assert.Equal(newsList.OrderByDescending(x => x.CreatedOn), listResult);
        }