c# - How to compare viewmodel and dependency in unit testing? -
below code of method :-
[httppost] public actionresult index(productviewmodel model) { if (model != null) { return partialview("_productgrid", getproduct(model)); } else { return redirecttoaction("index", "product"); } }
and below code unit testing method (in c#,mvc) :-
[testmethod] public void index_withmodel_posttest() { //arrange productcontroller controller = new productcontroller(); var model = new productviewmodel() { name="product1", description="desc" }; //act partialviewresult actual = controller.index(model) partialviewresult; if (actual != null) { var viewmodel = (productviewmodel)((viewresultbase)(actual)).model; int matches = _productservice.getdeals("", model.description).count + _productservice.getinsurance("", model.description).count + _productservice.getcategory("", model.description).count; //assert assert.isnotnull(actual); assert.isinstanceoftype(actual, typeof(partialviewresult)); assert.isinstanceoftype(viewmodel, typeof(productviewmodel)); assert.areequal("_productgrid", actual.viewname); assert.areequal(matches, viewmodel.products.count()); } }
you can see on below part of method fetching products
of 3 methods. of 3 methods productservice
dependency.and want know should mock
condition ? or can in other way ? want assert
count
of matches variable , actual.product.count
.
i think essence of unit testing take advantage of separation of concerns. u should use moq create mock data accessed created repository , call repository in controller , pass unit testing.the essence of separation of concerns , unit testing able test each layer separately.browse on moq
Comments
Post a Comment