mercredi 1 avril 2015

how to write a moq test to setup a method to return a count value

In my controller action, whenever a new product is added I check in the database that this product no is not present already. The code for this check looks like



public ActionResult Index(ProductModel model)
{
var productCount = _productsService.GetAll(true).Count(x => x.ProductNumber == model.ProductNumber);

if (productCount > 0)
ModelState.AddModelError("ProductNumber", Product already present in the system!");

// more processing
}


I m new to MOQ testing and trying to write a unit test to setup the GetAll method which will return 0. I have written something like this but it does not seem to work



var _productsService = new Mock<IProductsService>();
_productsService.Setup(m => m.GetAll(true).Count()).Returns(0);


Any ideas? thanks


Aucun commentaire:

Enregistrer un commentaire