Beispiel #1
0
        public async void Create_ShouldThrowArgumentException_IfNameIsNullOrWhiteSpace()
        {
            Mapper.Initialize(x => x.AddProfile <MapperConfiguration>());
            var repo = new Mock <IRepository <Category> >();

            var category = new Category {
                Name = "category"
            };

            repo.Setup(r => r.AddAsync(category));
            var service = new CategoriesService(repo.Object, null);

            //do
            var result = service.GetAll();

            var  expected = typeof(ArgumentException);
            Type actual   = null;

            try
            {
                await service.Create("");
            }
            catch (ArgumentException e)
            {
                actual = e.GetType();
            }

            //assert
            Assert.Equal(expected, actual);
        }
        public async Task CreateCategoryShouldReturnFalseForAlreadyExistingCategory()
        {
            var categoriesRepo = new Mock <IRepository <Category> >();
            var categories     = new List <Category>()
            {
                new Category {
                    Name = "Jackets"
                },
                new Category()
            };

            categoriesRepo.Setup(r => r.All()).Returns(categories.AsQueryable());

            var service = new CategoriesService(categoriesRepo.Object);

            var category = new Category {
                Name = "Jackets"
            };
            var result = await service.Create(category);

            Assert.False(result);
            Assert.Equal(2, categories.Count);
            categoriesRepo.Verify(r => r.AddAsync(category), Times.Never);
            categoriesRepo.Verify(r => r.SaveChangesAsync(), Times.Never);
        }
        public async Task CreateCategoryShouldWork()
        {
            var categoriesRepo = new Mock <IRepository <Category> >();
            var categories     = new List <Category>()
            {
                new Category(),
                new Category()
            };

            categoriesRepo.Setup(r => r.All()).Returns(categories.AsQueryable());
            categoriesRepo.Setup(r => r.AddAsync(It.IsAny <Category>())).Returns <Category>(Task.FromResult)
            .Callback <Category>(c => categories.Add(c));

            var service = new CategoriesService(categoriesRepo.Object);

            var category = new Category {
                Name = "Jackets"
            };
            var result = await service.Create(category);

            Assert.True(result);
            Assert.Equal(3, categories.Count);
            Assert.Contains(categories, c => c.Name == category.Name);
            categoriesRepo.Verify(r => r.AddAsync(category), Times.Once);
            categoriesRepo.Verify(r => r.SaveChangesAsync(), Times.Once);
        }
        public void Throw_WhenPassedParameterIsNull()
        {
            //Arrange
            var categories        = new Mock <IEfGenericRepository <Category> >();
            var categoriesService = new CategoriesService(categories.Object);

            //Act & Assert
            Assert.Throws <ArgumentNullException>(() => categoriesService.Create(null));
        }
Beispiel #5
0
 public ActionResult <Category> Post([FromBody] Category newCategory)
 {
     try
     {
         var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
         newCategory.UserId = userId;
         return(Ok(_cs.Create(newCategory)));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Beispiel #6
0
        public void Create_ShouldCreateACategoryAndReturnTheId()
        {
            Mapper.Initialize(x => x.AddProfile <MapperConfiguration>());
            var repo = new Mock <IRepository <Category> >();

            var service = new CategoriesService(repo.Object, null);

            //do
            var result = service.Create("category");

            //assert
            result.Should().NotBeNull().And.BeOfType <Task <int> >();
        }
        public void InvokeRepositoryMethodAddOnce_WhenPassedParameterIsValid()
        {
            //Arrange
            var categories = new Mock <IEfGenericRepository <Category> >();

            categories.Setup(x => x.Add(It.IsAny <Category>())).Verifiable();
            var categoriesService = new CategoriesService(categories.Object);
            var category          = DataHelper.GetCategory();

            //Act
            categoriesService.Create(category);

            //Assert
            categories.Verify(x => x.Add(It.IsAny <Category>()), Times.Once);
        }
Beispiel #8
0
        public async Task CreateArticleShouldBeSuccessfull()
        {
            var options = new DbContextOptionsBuilder <SportsNewsContext>()
                          .UseInMemoryDatabase(databaseName: "CategoryTests")
                          .Options;

            var dbContext = new SportsNewsContext(options);

            var repository        = new DbRepository <Category>(dbContext);
            var categoriesService = new CategoriesService(repository);

            await categoriesService.Create("Football", "dsdaasd");

            var count = repository.All().Count();

            Assert.Equal(1, count);
        }
Beispiel #9
0
        public ActionResult AddCategory(CategoryViewModel model)
        {
            var result = _categoriesService.Get(model.Id);

            if (result == null)
            {
                var res = _categoriesService.Create(model.ToBll()).ToViewModel();

                if (res == null)
                {
                    return(this.HttpResult(HttpStatusCode.Conflict));
                }

                return(this.Json(res, JsonRequestBehavior.AllowGet));
            }

            return(this.HttpResult(HttpStatusCode.OK));
        }
Beispiel #10
0
        public async Task CategoryShouldBeValid()
        {
            var options = new DbContextOptionsBuilder <SportsNewsContext>()
                          .UseInMemoryDatabase(databaseName: "CategoryTests2")
                          .Options;

            var dbContext = new SportsNewsContext(options);

            var repository        = new DbRepository <Category>(dbContext);
            var categoriesService = new CategoriesService(repository);

            await categoriesService.Create("Basket", "dsasddas");

            var categoryId = repository.All().FirstOrDefault().Id;
            var isValid    = categoriesService.IsCategoryIdValid(categoryId);

            Assert.True(isValid);
        }
        public async Task <IActionResult> Create([Required, FromQuery] string name)
        {
            var response = await _service.Create(name);

            if (response.Status == BaseResponse.Statuses.Failed)
            {
                return(BadRequest(new
                {
                    Errors = new[] { new ErrorModel {
                                         Field = nameof(name), Errors = new[] { response.ErrorMessage }
                                     } }
                }));
            }
            return(Created("", new
            {
                Data = response
            }));
        }