public async Task <IActionResult> Add(FabricCreationBindingModel model) { var fabric = await this.fabricsService.AddFabricAsync(model); SetSuccesfullMessage(AddedSuccessfully, FabricConst); return(this.RedirectToAction("Details", new { id = fabric.Id, slug = fabric.Slug })); }
public async Task AddFabric_WithNullFabric_ShouldThrowExeption() { // Arrange FabricCreationBindingModel fabricModel = null; // Act Func <Task> addFabric = () => this.service.AddFabricAsync(fabricModel); // Assert var exception = await Assert.ThrowsExceptionAsync <ArgumentException>(addFabric); Assert.AreEqual(ValidationConstants.FabricDefinedMessage, exception.Message); }
public async Task <Fabric> AddFabricAsync(FabricCreationBindingModel model) { Validator.EnsureNotNull(model, ValidationConstants.FabricDefinedMessage); Validator.EnsureStringNotNullOrEmpty(model.Name, ValidationConstants.FabricNameMessage); Validator.EnsureStringNotNullOrEmpty(model.Slug, ValidationConstants.FabricSlugMessage); Validator.EnsureStringNotNullOrEmpty(model.Description, ValidationConstants.FabricDescriptionMessage); Validator.EnsureStringNotNullOrEmpty(model.ImageUrl, ValidationConstants.FabricImageUrlMessage); var fabric = this.Mapper.Map <Fabric>(model); await this.DbContext.Fabrics.AddAsync(fabric); await this.DbContext.SaveChangesAsync(); return(fabric); }
public void Add_WithValidFabric_ShouldCallService() { // Arrange var model = new FabricCreationBindingModel(); bool serviceCalled = false; var mockRepository = new Mock <IAdminFabricsService>(); mockRepository.Setup(repo => repo.AddFabricAsync(model)) .Callback(() => serviceCalled = true); var controller = new FabricsController(mockRepository.Object); // Act var result = controller.Add(model); // Assert Assert.IsTrue(serviceCalled); }
public async Task AddFabric_WithMissingSlug_ShouldThrowExeption() { // Arrange FabricCreationBindingModel fabricModel = new FabricCreationBindingModel() { Name = fabricName, Slug = null, Description = fabricDescription, ImageUrl = fabricImageUrl }; // Act Func <Task> addFabric = () => this.service.AddFabricAsync(fabricModel); // Assert var exception = await Assert.ThrowsExceptionAsync <ArgumentException>(addFabric); Assert.AreEqual(ValidationConstants.FabricSlugMessage, exception.Message); }
public async Task AddFabric_WithProperFabric_ShouldAddCorrectly() { // Arrange var fabricModel = new FabricCreationBindingModel() { Name = fabricName, Slug = fabricSlug, Description = fabricDescription, ImageUrl = fabricImageUrl }; // Act await this.service.AddFabricAsync(fabricModel); var fabric = this.dbContext.Fabrics.First(); // Assert Assert.AreEqual(1, this.dbContext.Fabrics.Count()); Assert.AreEqual(fabricName, fabric.Name); Assert.AreEqual(fabricSlug, fabric.Slug); Assert.AreEqual(fabricDescription, fabric.Description); Assert.AreEqual(fabricImageUrl, fabric.ImageUrl); }