public void Cannot_Save_Invalid_Changes() { //---Arrange Mock <IRepository> mock = new Mock <IRepository>(); mock.Setup(b => b.Brands).Returns(new Brands[] { new Brands() { BrandId = 1, BrandName = "B1" } }); BrandsController controller = new BrandsController(mock.Object); Brands brand = new Brands() { BrandName = "Test" }; controller.ModelState.AddModelError("error", "error"); //---Act ActionResult result = controller.Edit(brand); //Assert mock.Verify(b => b.SaveBrands(It.IsAny <Brands>()), Times.Never()); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void Can_Delete_Brand() { //---Arrange Brands brand = new Brands() { BrandId = 2, BrandName = "B2" }; Mock <IRepository> mock = new Mock <IRepository>(); mock.Setup(b => b.Brands).Returns(new Brands[] { new Brands() { BrandId = 1, BrandName = "B1" }, brand }); BrandsController controller = new BrandsController(mock.Object); //---Act ActionResult result = controller.Delete(brand.BrandId); //---Assert mock.Verify(b => b.DeleteBrand(brand.BrandId)); }
public async Task BrandsController_Put_Success() { var createRequest = new BrandCreateRequest { Name = "Test brand5" }; var controller = new BrandsController(_fixture.BrandService); var createResult = await controller.Post(createRequest); var createResponse = GetResponse <BrandCreateResponse>(createResult); Assert.Equal(createRequest.Name, createResponse.Name); var updateRequest = new BrandUpdateRequest { Id = createResponse.Id, Name = "New Name" }; var updateResult = await controller.Put(updateRequest); var updateResponse = GetResponse <BrandUpdateResponse>(updateResult); Assert.Equal(updateRequest.Id, updateResponse.Id); Assert.Equal(updateRequest.Name, updateResponse.Name); }
public async void UpdateBrand_ReturnBrandUpdated(int id) { // Arrange var mockRepo = new Mock <IVehicleCatalogRepository>(); mockRepo.Setup(repo => repo.GetBrand(id)) .Returns(Task.FromResult <Brand>(new Brand() { Id = id, Name = "Brand 1" })); mockRepo.Setup(repo => repo.SaveAll()) .Returns(Task.FromResult <bool>(true)); var mapper = _dependencyFixture.ServiceProvider.GetService <IMapper>(); var logger = Mock.Of <ILogger <BrandsController> >(); var controller = new BrandsController(mockRepo.Object, mapper, logger); BrandForUpdate newBrand = new BrandForUpdate() { name = "Fake" }; // Act var result = await controller.Put(id, newBrand); // Assert var okResult = Assert.IsType <CreatedAtRouteResult>(result); var returnValue = Assert.IsType <string>(okResult.Value); Assert.Equal("Brand updated.", returnValue); }
public async Task BrandsController_GetAll_Success() { var brands = new List <BrandCreateRequest> { new BrandCreateRequest { Name = "Test brand1" }, new BrandCreateRequest { Name = "Test brand2" } }; var controller = new BrandsController(_fixture.BrandService); foreach (var brand in brands) { await controller.Post(brand); } var result = await controller.GetAll(); var brandVms = GetCollectionResponse <BrandVm>(result); Assert.NotNull(brandVms); foreach (var brand in brands) { Assert.Contains(brandVms, x => x.Name == brand.Name); } }
public async void QueryBrandList_ReturnsBrandsAscendingByName() { // Arrange var mockRepo = new Mock <IVehicleCatalogRepository>(); mockRepo.Setup(repo => repo.GetBrands()) .Returns(Task.FromResult <IEnumerable <Brand> >(new List <Brand>() { new Brand() { Id = 1, Name = "Crand 1" }, new Brand() { Id = 2, Name = "Brand 2" }, new Brand() { Id = 3, Name = "Drand 3" }, }.OrderBy(o => o.Name))); var mapper = _dependencyFixture.ServiceProvider.GetService <IMapper>(); var logger = Mock.Of <ILogger <BrandsController> >(); var controller = new BrandsController(mockRepo.Object, mapper, logger); // Act var result = await controller.GetBrands(); // Assert var okResult = Assert.IsType <OkObjectResult>(result); var returnValue = Assert.IsType <List <BrandForList> >(okResult.Value); var topBrand = returnValue.FirstOrDefault().name; Assert.Equal("Brand 2", topBrand); }
public void List_Contain_Brands() { //---Arrange--- Mock <IRepository> mock = new Mock <IRepository>(); mock.Setup(b => b.Brands).Returns(new Brands[] { new Brands { BrandId = 1, BrandName = "B1" }, new Brands { BrandId = 2, BrandName = "B2" }, new Brands { BrandId = 3, BrandName = "B3" }, }); BrandsController controller = new BrandsController(mock.Object); //---Act--- var result = ((ViewResult)controller.List()).ViewData.Model as Brands[]; //---Assert--- Assert.AreEqual(3, result.Length); Assert.AreEqual(1, result[0].BrandId); Assert.AreEqual(2, result[1].BrandId); Assert.AreEqual(3, result[2].BrandId); }
public void Can_Edit_Brand() { //---Arrange--- Mock <IRepository> mock = new Mock <IRepository>(); mock.Setup(b => b.Brands).Returns(new Brands[] { new Brands { BrandId = 1, BrandName = "B1" }, new Brands { BrandId = 2, BrandName = "B2" }, new Brands { BrandId = 3, BrandName = "B3" }, }); BrandsController controller = new BrandsController(mock.Object); //---Act--- var brand1 = ((ViewResult)controller.Edit(1)).ViewData.Model as Brands; var brand2 = ((ViewResult)controller.Edit(2)).ViewData.Model as Brands; var brand3 = ((ViewResult)controller.Edit(3)).ViewData.Model as Brands; //---Assert--- Assert.AreEqual(1, brand1.BrandId); Assert.AreEqual(2, brand2.BrandId); Assert.AreEqual(3, brand3.BrandId); }
public async Task GetUpdateShouldReturnViewWithValidModel() { // Arrange var brandDb = DataHelper.GetBrand(); var adminBrandsService = new Mock <IAdminBrandsService>(); adminBrandsService .Setup(s => s.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(brandDb); var controller = new BrandsController(adminBrandsService.Object, null); // Act var result = await controller.Update(brandDb.Id); // Assert result.Should().NotBeNull(); result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().ViewName.Should().BeNull(); var model = result.As <ViewResult>().Model; model.Should().BeOfType <BrandFormModel>(); var formModel = model.As <BrandFormModel>(); formModel.Id.Should().Be(brandDb.Id); formModel.Name.Should().Be(brandDb.Name); formModel.WebSite.Should().Be(brandDb.WebSite); formModel.Admin.Name.Should().Be(brandDb.Admin.Name); formModel.AdminId.Should().Be(brandDb.AdminId); }
public async Task IndexShouldReturnsViewWithCorrectModel() { // Arrange var brands = DataHelper.GetBrandsCollection(); var adminBrandsService = new Mock <IAdminBrandsService>(); adminBrandsService .Setup(s => s.AllAsync()) .ReturnsAsync(brands); var controller = new BrandsController(adminBrandsService.Object, null); // Act var result = await controller.Index(); // Assert result.Should().NotBeNull(); result.Should().BeOfType <ViewResult>(); var viewName = result.As <ViewResult>().ViewName; viewName.Should().BeNull(); var model = result.As <ViewResult>().Model; var viewModel = model.As <BrandListingViewModel>(); viewModel.Should().NotBeNull(); }
public void GetReturnsBrand() { var controller = new BrandsController(); var mock = new Mock <CarContext>(); mock.Setup(p => p.Brands.Find(1)); BrandsController brandsController = new BrandsController(); var result = brandsController.Details(1); }
public void ReturnDefaultView_WhenGetToIndex() { // Arrange var brandsService = new Mock <IBrandsService>(); var controller = new BrandsController(brandsService.Object); // Act && Assert controller.WithCallTo(c => c.Index()).ShouldRenderDefaultView(); }
public async Task BrandMethod() { // Arrange BrandsController controller = new BrandsController(); // Act Task result = controller.GetBrand() as Task; // Assert Assert.IsNotNull(result); }
public void ReturnsAnInstance_WhenParameterIsNotNull() { // Arrange var brandsService = new Mock <IBrandsService>(); // Act var controller = new BrandsController(brandsService.Object); // Assert Assert.IsInstanceOf <BrandsController>(controller); }
public void TestInitialize() { _mockBrandsReposiory = new Mock <IBrandsRepository>(); var mockUoW = new Mock <IUnitOfWork>(); mockUoW.SetupGet(u => u.Brands).Returns(_mockBrandsReposiory.Object); _brandsController = new BrandsController(mockUoW.Object); _brandsController.MockCurrentUser(_userId, _userName); }
public void Initialize() { brandDto = new BrandDto { Id = existedBrandId }; brandServiceMock = new Mock <IBrandService>(); brandServiceMock.Setup(x => x.Get(existedBrandId)) .Returns(brandDto); brandController = new BrandsController(brandServiceMock.Object); }
public async Task BrandsController_Post_Success() { var brand = new BrandCreateRequest { Name = "Test brand4" }; var controller = new BrandsController(_fixture.BrandService); var result = await controller.Post(brand); var response = GetResponse <BrandCreateResponse>(result); Assert.Equal(brand.Name, response.Name); }
public async Task GetAllBrands_ShouldReturnList() { // Act var response = await Client.GetAsync("api/brands"); await response.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var controller = new BrandsController(_brandRepository); var result = await controller.Get(); Assert.True(result.Count > 0); }
public async Task GetCategory_Success() { _applicationDbContex.Brands.Add(new Brand { Name = "Test category" }); await _applicationDbContex.SaveChangesAsync(); var controller = new BrandsController(_applicationDbContex); var result = await controller.GetBrands(); var actionResult = Assert.IsType <ActionResult <IEnumerable <BrandVm> > >(result); Assert.NotEmpty(actionResult.Value); }
public void Delete_ShouldReturnOk() { //Arrange var mock = new Mock <IBrandService>(); var controller = new BrandsController(mock.Object); //Act mock.Setup(p => p.Delete(1)); var result = controller.Delete(1) as OkResult; // Assert Assert.IsInstanceOfType(result, typeof(OkResult)); Assert.AreEqual(200, result.StatusCode); }
public async Task PostBrand_Success() { var brand = new BrandCreateRequest { Name = "Test brand" }; var controller = new BrandsController(_applicationDbContex); var result = await controller.PostBrand(brand); var createdAtActionResult = Assert.IsType <CreatedAtActionResult>(result.Result); var returnValue = Assert.IsType <BrandVm>(createdAtActionResult.Value); Assert.Equal("Test brand", returnValue.Name); }
public void Get_ShouldReturnAllBrandsAndTotalProducts() { //Arrange var mock = new Mock <IBrandService>(); var controller = new BrandsController(mock.Object); //Act mock.Setup(p => p.GetBrandsAndTotalProducts()); var result = controller.GetBrandsAndTotalProducts() as OkObjectResult; // Assert Assert.IsNotNull(result); Assert.AreEqual(200, result.StatusCode); }
public async Task GetProductBrandAsync_ShouldReturnAListOfProductBrand() { // Arrange var sut = new BrandsController(_brandRepo); // Act var productBrand = await sut.GetProductBrand(); // Assert var actionResult = Assert.IsType <ActionResult <IReadOnlyList <ProductBrand> > >(productBrand); var productBrandList = Assert.IsAssignableFrom <IReadOnlyList <ProductBrand> >(((OkObjectResult)actionResult.Result).Value); Assert.Equal(_fixture.SeedEntries, productBrandList.Count); }
public void ReturnJsonResult_WhenGetToBrands_Create() { //Arrange var brandsService = new Mock <IBrandsService>(); var brandViewModel = DataHelper.GetAdminBrandViewModel(); var kendoDataRequest = new DataSourceRequest(); brandsService.Setup(x => x.Create(It.IsAny <Brand>())).Verifiable(); var controller = new BrandsController(brandsService.Object); //Act & Assert controller.WithCallTo(c => c.Brands_Create(kendoDataRequest, brandViewModel)).ShouldReturnJson(); }
public void ReturnJsonResult_WhenGetToBrands_Read() { //Arrange var brandsService = new Mock <IBrandsService>(); var brands = DataHelper.GetBrands(); var kendoDataRequest = new DataSourceRequest(); brandsService.Setup(x => x.GetAll()).Returns(brands); var controller = new BrandsController(brandsService.Object); //Act & Assert controller.WithCallTo(c => c.Brands_Read(kendoDataRequest)).ShouldReturnJson(); }
public void GetAllBrands_ShouldReturnAllBrands() { //Arrange var testBrands = BrandBuilderBuilder.GetTestBrands(); var mock = new Mock <IBrandService>(); //Act mock.Setup(p => p.Get()).Returns(testBrands); var controller = new BrandsController(mock.Object); var result = controller.Get() as OkObjectResult; //Assert Assert.IsNotNull(result); Assert.AreEqual(200, result.StatusCode); }
public async Task GetBrand_Success() { var dbContext = _fixture.Context; await dbContext.Brands.AddRangeAsync(new Brand { Name = "Test1 brand" }, new Brand { Name = "Test2 brand" }, new Brand { Name = "Test3 brand" }); await dbContext.SaveChangesAsync(); var brandService = new BrandService(dbContext); var brandsController = new BrandsController(brandService); var result = await brandsController.GetBrands(); var actionResult = Assert.IsType <OkObjectResult>(result.Result); Assert.NotEmpty(actionResult.Value as IEnumerable <BrandVm>); }
public void IndexPageShouldWorkCorrectlyPassingBrandName() { IQueryable<Product> productsAsQueryable = this.products.AsQueryable(); productServiceMock.Setup(h => h.GetAllByBrand("Adidas", null)) .Returns(productsAsQueryable.Where(p => p.Brand.Name == "Adidas")); var controller = new BrandsController(productServiceMock.Object); controller.WithCallTo(h => h.Index("Adidas", 1, null, "Likes", "Desc")) .ShouldRenderPartialView("_ProductsPartial") .WithModel<PaginationViewModel>(viewModel => { Assert.AreEqual(1, viewModel.Products.Count); }) .AndNoModelErrors(); }
public void GetCreateShouldReturnView() { // Arrange var controller = new BrandsController(null, null); // Act var result = controller.Create(); // Assert result.Should().NotBeNull(); result.Should().BeOfType <ViewResult>(); var viewName = result.As <ViewResult>().ViewName; viewName.Should().BeNull(); }
public async Task ValidatePostBrand() { var brand = new BrandCreateRequest() { Name = "GOG.com" }; var controller = new BrandsController(_dbContext); var result = await controller.PostBrand(brand); var createdAtActionResult = Assert.IsType <CreatedAtActionResult>(result.Result); var returnValue = Assert.IsType <BrandVm>(createdAtActionResult.Value); Assert.Equal("GOG.com", returnValue.Name); }
public async Task GetBrand_Success() { var dbContext = _fixture.Context; dbContext.Brands.Add(new Brand { BrandName = "TestGetBrand" }); await dbContext.SaveChangesAsync(); var controller = new BrandsController(dbContext); var result = await controller.GetBrand(); var actionResult = Assert.IsType <ActionResult <IEnumerable <BrandVm> > >(result); Assert.NotEmpty(actionResult.Value); }
public void IndexPageShouldWorkCorrectlyPassingQuery() { const string Query = "Product 2"; IQueryable<Product> productsAsQueryable = this.products.AsQueryable(); productServiceMock.Setup(h => h.GetAllByBrand(null, Query)) .Returns(productsAsQueryable.Where(p => p.Title.Contains(Query))); var controller = new BrandsController(productServiceMock.Object); controller.WithCallTo(h => h.Index(null, 1, Query, "Likes", "Desc")) .ShouldRenderPartialView("_ProductsPartial") .WithModel<PaginationViewModel>(viewModel => { Assert.AreEqual(1, viewModel.Products.Count); }) .AndNoModelErrors(); }
public void IndexPageShouldWorkCorrectlyPassingSortByLikesAsc() { const string SortBy = "Likes"; IQueryable<Product> productsAsQueryable = this.products.AsQueryable(); productServiceMock.Setup(h => h.GetAllByBrand(null, null)) .Returns(productsAsQueryable); var controller = new BrandsController(productServiceMock.Object); controller.WithCallTo(h => h.Index(null, 1, null, SortBy, "Asc")) .ShouldRenderPartialView("_ProductsPartial") .WithModel<PaginationViewModel>(viewModel => { Assert.AreEqual("Product 3", viewModel.Products.FirstOrDefault().Title); }) .AndNoModelErrors(); }