Unit-testing model changes in Asp.net MVC 3

I have a class ProductControllerand Product. Each time an action is called Createin ProductController, it creates a new Productone based on FormCollection, and then calls a function inside Productto change the date:

[HttpPost]
public ActionResult Create(FormCollection form)
{
    Product product = new Product();
    TryUpdateModel(product, form);

    if(ModelState.IsValid)
    {
        product.ChangeDate(form["date"]);
        repository.SaveProduct(product);

        return RedirectToAction("Index");
    }
    else
        return View();
}

I was wondering how I can check Product, so I know what .ChangeDateis being called (via Moq Verify). I did not use the automatic binding of the Asp.Net model because I want to catch any binding exception through TryUpdateModel. I'm not sure if I should put .ChangeDatein a Controller or Repository class. I am using Moq, MVC3 and Entity Framework 4. Any help is appreciated!

Thanks Alex

+3
source
1

SaveProduct , . , . , Date , Create .

var mock = new Mock<IRepository>();
mock.Setup(
    x => x.SaveProduct(
        It.Is<Product>(p => p.Date == someExpectedDate)
    )
);
+3

Source: https://habr.com/ru/post/1787302/


All Articles