public ActionResult Update(StockUpdateViewData stockUpdateViewData)
        {
            if (stockUpdateViewData == null)
            {
                throw new ArgumentNullException("stockUpdateViewData");
            }

            foreach (var updateItem in stockUpdateViewData.UpdateItems)
            {
                var stockItem = stockItemService.GetById(updateItem.StockItemId);
                if (updateItem.HasReceivedValue())
                {
                    stockItem.ReceiveStock(updateItem.GetReceivedValue(), now(), currentUser())
                        .SetComment(stockUpdateViewData.Comment);
                }
                if (updateItem.HasAdjustedValue())
                {
                    stockItem.AdjustStockLevel(updateItem.GetAdjustmentValue(), now(), currentUser())
                        .SetComment(stockUpdateViewData.Comment);
                }
                if (updateItem.IsInStock != stockItem.IsInStock)
                {
                    if (updateItem.IsInStock)
                    {
                        stockItem.SetInStock(now(), currentUser()).SetComment(stockUpdateViewData.Comment);
                    }
                    else
                    {
                        stockItem.SetOutOfStock(now(), currentUser()).SetComment(stockUpdateViewData.Comment);
                    }
                }
            }

            return Redirect(stockUpdateViewData.ReturnUrl);
        }
        public void Update_should_update_stock()
        {
            // create the view data posted from the form
            const string returnUrl = "http://return.url.com/something";
            const string comment= "some comment";

            var stockUpdateViewData = new StockUpdateViewData
            {
                ReturnUrl = returnUrl,
                Comment = comment,
                UpdateItems =
                    {
                        new UpdateItem
                        {
                            StockItemId    = 4,
                            Received = "10",
                            Adjustment = "",
                            IsInStock = true
                        },
                        new UpdateItem
                        {
                            StockItemId    = 5,
                            Received = "",
                            Adjustment = "22",
                            IsInStock = false
                        },
                        new UpdateItem
                        {
                            StockItemId    = 6,
                            Received = "",
                            Adjustment = "",
                            IsInStock = true
                        }
                    }
            };

            // create some stock items
            var dateCreated = new DateTime(2010, 6, 11);
            const string createdByUser = "[email protected]";
            var stockItems = new List<StockItem>
            {
                StockItem.Create("Widget", "Small", dateCreated, createdByUser).SetId(4),
                StockItem.Create("Widget", "Med", dateCreated, createdByUser).SetId(5),
                StockItem.Create("Widget", "Large", dateCreated, createdByUser).SetId(6),
                StockItem.Create("Widget", "Huge", dateCreated, createdByUser).SetId(7),
            };
            stockItems[0].ReceiveStock(10, dateCreated, createdByUser);
            stockItems[1].ReceiveStock(10, dateCreated, createdByUser);
            stockItems[2].ReceiveStock(10, dateCreated, createdByUser);
            stockItems[3].ReceiveStock(10, dateCreated, createdByUser);

            stockItemService.GetByIdDelegate = id => stockItems.Single(x => x.Id == id);

            // run the controller Update action
            var result = controller.Update(stockUpdateViewData).ReturnsRedirect();
            result.Url.ShouldEqual(returnUrl);

            // assert that stock levels are as expected
            stockItems[0].Level.ShouldEqual(20);
            stockItems[0].History[2].Comment.ShouldEqual(comment);
            stockItems[1].Level.ShouldEqual(22);
            stockItems[1].History[2].Comment.ShouldEqual(comment);
            stockItems[2].Level.ShouldEqual(10);
            stockItems[3].Level.ShouldEqual(10);

            // assert that stock item IsInStock is as expected
            stockItems[0].IsInStock.ShouldBeTrue();
            stockItems[1].IsInStock.ShouldBeFalse();
            stockItems[2].IsInStock.ShouldBeTrue();
            stockItems[3].IsInStock.ShouldBeTrue();
        }