开发者

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 HomeContr开发者_StackOverflow中文版ollerInsert_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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜