public void Handle_Changes_All_Product_Categories()
        {
            //Arrange
            const int catId = 3;
            var category = new Category {Name = "sharp"};
            category.SetIdTo(catId);
            var productIds = new List<int> { 2, 3, 4 };
            var product1 = new Product { Category = new Category() };
            product1.SetIdTo(2);
            var product2 = new Product { Category = new Category() };
            product2.SetIdTo(3);
            var product3 = new Product { Category = new Category() };
            product3.SetIdTo(4);
            _categoryTasks.Expect(x => x.Get(catId)).Return(category);
            _productTasks.Expect(x => x.Get(2)).Return(product1);
            _productTasks.Expect(x => x.Get(3)).Return(product2);
            _productTasks.Expect(x => x.Get(4)).Return(product3);
            _productTasks.Expect(x => x.CreateOrUpdate(Arg<Product>.Matches(y => y.Id == 2 && y.Category.Name == "sharp"))).Return(product1);
            _productTasks.Expect(x => x.CreateOrUpdate(Arg<Product>.Matches(y => y.Id == 3 && y.Category.Name == "sharp"))).Return(product2);
            _productTasks.Expect(x => x.CreateOrUpdate(Arg<Product>.Matches(y => y.Id == 4 && y.Category.Name == "sharp"))).Return(product3);

            //Act
            _handler.Handle(new MassCategoryChangeCommand(catId, productIds));

            //Assert
            _categoryTasks.VerifyAllExpectations();
            _productTasks.VerifyAllExpectations();
        }
 public Category CreateOrUpdate(Category category)
 {
     if(category.Id == 0)
         category.Created = DateTime.Now;
     category.Modified = DateTime.Now;
     _categoryRepository.SaveOrUpdate(category);
     return category;
 }
        public void Constructor_Sets_Defaults()
        {
            //Act
            var category = new Category();

            //Assert
            Assert.AreEqual(0, category.Products.Count);
        }
        public void Delete_Entity_Deletes_Category()
        {
            //Arrange
            var category = new Category();
            _repository.Expect(x => x.Delete(category));

            //Act
            _tasks.Delete(category);

            //Assert
            _repository.VerifyAllExpectations();
        }
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid && category.IsValid())
            {
                _tasks.CreateOrUpdate(category);
                return this.RedirectToAction(x => x.Index());
            }

            if (category.Id == 0)
                return View("Create", category);
            return View(category);
        }
        public void Delete_Deletes_Product_And_Redirects_To_Index()
        {
            //Arrange
            var category = new Category();
            category.SetIdTo(3);
            _tasks.Expect(x => x.Delete(3));

            //Act
            var result = _controller.Delete(3) as RedirectToRouteResult;

            //Assert
            Assert.AreEqual("Index", result.RouteValues["Action"]);
            _tasks.VerifyAllExpectations();
        }
        public void CreateOrUpdate_Does_Not_Set_Created_With_Existing_And_Saves()
        {
            //Arrange
            var category = new Category();
            category.SetIdTo(2);
            _repository.Expect(x => x.SaveOrUpdate(category));

            //Act
            var updated = _tasks.CreateOrUpdate(category);

            //Assert
            Assert.AreEqual(DateTime.MinValue, updated.Created.Date);
            _repository.VerifyAllExpectations();
        }
        public void AddProduct_Sets_Category_And_Adds_Product()
        {
            //Arrange
            var product = new Product();
            var category = new Category();
            category.SetIdTo(4);

            //Act
            category.AddProduct(product);

            //Assert
            Assert.AreEqual(1, category.Products.Count);
            Assert.AreEqual(4, product.Category.Id);
        }
        public void Delete_Id_Deletes_Category()
        {
            //Arrange
            const int Id = 4;
            var category = new Category();
            category.SetIdTo(Id);
            _repository.Expect(x => x.Get(Id)).Return(category);
            _repository.Expect(x => x.Delete(category));

            //Act
            _tasks.Delete(Id);

            //Assert
            _repository.VerifyAllExpectations();
        }
        public void CreateOrUpdate_Sets_Created_And_Modified_And_Saves()
        {
            //Arrange
            var category = new Category();
            _repository.Expect(x => x.SaveOrUpdate(category));

            //Act
            var updated = _tasks.CreateOrUpdate(category);

            //Assert
            var now = DateTime.Now;
            Assert.AreEqual(now.Date, updated.Modified.Date);
            Assert.AreEqual(now.Date, updated.Created.Date);
            _repository.VerifyAllExpectations();
        }
        public void Edit_Forwards_To_View_With_Category()
        {
            //Arrange
            var category = new Category();
            category.SetIdTo(3);
            _tasks.Expect(x => x.Get(3)).Return(category);

            //Act
            var result = _controller.Edit(3) as ViewResult;

            //Assert
            Assert.AreEqual(string.Empty, result.ViewName);
            Assert.IsInstanceOfType<Category>(result.Model);
            Assert.AreEqual(3, (result.Model as Category).Id);
            _tasks.VerifyAllExpectations();
        }
        public void Handle_Changes_Does_Not_Change_Category_If_Same()
        {
            //Arrange
            const int catId = 3;
            var category = new Category {Name = "sharp"};
            category.SetIdTo(catId);
            var productIds = new List<int> { 2 };
            var product1 = new Product {Category = category};
            product1.SetIdTo(2);
            _categoryTasks.Expect(x => x.Get(catId)).Return(category);
            _productTasks.Expect(x => x.Get(2)).Return(product1);
            _productTasks.AssertWasNotCalled(x => x.CreateOrUpdate(Arg<Product>.Is.Anything));

            //Act
            _handler.Handle(new MassCategoryChangeCommand(catId, productIds));

            //Assert
            _categoryTasks.VerifyAllExpectations();
            _productTasks.VerifyAllExpectations();
        }
        public void Edit_Validates_Bad_Model_And_Forwards_To_Edit()
        {
            //Arrange
            var routeData = new RouteData();
            var httpContext = MockRepository.GenerateStub<HttpContextBase>();
            var controllerContext = MockRepository.GenerateStub<ControllerContext>(httpContext, routeData, _controller);
            _controller.ControllerContext = controllerContext;
            _controller.ValueProvider = new FormCollection().ToValueProvider();
            var category = new Category();
            category.SetIdTo(2);

            //Act
            var result = _controller.Edit(category) as ViewResult;

            //Assert
            Assert.AreEqual(string.Empty, result.ViewName);
            Assert.IsInstanceOfType<Category>(result.Model);
            Assert.AreEqual(2, (result.Model as Category).Id);
        }
        public void Get_Returns_Category_With_Correct_Id()
        {
            //Arrange
            const int Id = 4;
            var category = new Category();
            category.SetIdTo(Id);
            _repository.Expect(x => x.Get(Id)).Return(category);

            //Act
            var gotten = _tasks.Get(Id);

            //Assert
            Assert.AreEqual(Id, gotten.Id);
            _repository.VerifyAllExpectations();
        }
 public void Delete(Category category)
 {
     _categoryRepository.Delete(category);
 }