简体   繁体   中英

Testing TryUpdateModel() with MStest and moq

Im currently trying to test an insert method which uses TryUpdateModel(). I am faking the controllercontext which is needed and although that works it does not seem to be posting the model I have setup.

Here is the method I am testing:

 [AcceptVerbs(HttpVerbs.Post)]
    [GridAction]
    public ActionResult _SaveAjaxEditing(int? id)
    {
        if (id == null)
        {
            Product product = new Product();
            if (TryUpdateModel(product))
            {
                //The model is valid - insert the product.
                productsRepository.Insert(product);// AddToProducts(product);
            }
        }
        else
        {
            var recordToUpdate = productsRepository.Products.First(m => m.ProductID == id);
            TryUpdateModel(recordToUpdate);
        }
        productsRepository.Save();
        return View(new GridModel(productsRepository.Products.ToList()));
    }

And here is my current test:

        [TestMethod]
    public void HomeControllerInsert_ValidProduct_CallsInsertForProducts()
    {
        //Arrange
        InitaliseRepository();

        var httpContext = CustomMockHelpers.FakeHttpContext();
        var context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
        controller.ControllerContext = context;
        //controller.ControllerContext = new ControllerContext();

        var request = Mock.Get(controller.Request);
        request.Setup(r => r.Form).Returns(delegate()
                                               {
                                                   var prod = new NameValueCollection
                                                                  {
                                                                      {"ProductID", "9999"},
                                                                      {"Name", "Product Test"},
                                                                      {"Price", "1234"},
                                                                      {"SubCatID", "2"}
                                                                  };
                                                   return prod;
                                               });


        // Act: ... when the user tries to delete that product
        controller._SaveAjaxEditing(null);
        //Assert
        _mockProductsRepository.Verify(x => x.Insert(It.IsAny<Product>()));
    }

The method is being called but when it gets to TryUpdateModel() it seems it cannot pickup the posted object. Any pointers on where I am going wrong would be great.

Sorted it. Seems mocking the Httpcontext completly was overkill.

controller.ControllerContext = new ControllerContext();

var prod = new FormCollection
            {
                {"ProductID", "1"},
                {"Name", "Product Test"},
                {"Price", "1234"},
                {"SubCatID", "2"}
            };

controller.ValueProvider = prod.ToValueProvider();

This does the trick. It is now posted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM