c# - ASP.NET MVC moq returns empty list -
i'm using moq framework test mvc application. i've added generic repository , unit of work class. when run test of controller fails because mock returns empty list (size 0) though i've added 2 elements. here test:
[testmethod] public void index() { var repmock = new mock<ifakultetrepository<students>>(); var students = new list<students>(); students.add(new students() { bi = "10011", ime = "pera", prezime = "peric", adresa = "ulica1", grad = "grad1"}); students.add(new students() { bi = "20011", ime = "marko", prezime = "markovic", adresa = "ulica2", grad = "grad2" }); repmock.setup(x => x.getentities()).returns(students.topagedlist(1, 5)); studentscontroller controller = new studentscontroller(repmock.object); viewresult result = controller.index("", "test", "test", 1) viewresult; var listresult = result.viewdata.model pagedlist<students>; var list = listresult.tolist(); assert.areequal(2, list.count); }
here generic repository implements generic repository interface:
public class fakultetrepository<tentity> : ifakultetrepository<tentity> tentity : class { internal fakultetentities context; internal dbset<tentity> dbset; public fakultetrepository(fakultetentities context) { this.context = context; this.dbset = context.set<tentity>(); } public virtual ienumerable<tentity> getentities() { return dbset.tolist(); } public virtual tentity getentitybyid(object id) { return dbset.find(id); } public virtual tentity getentitybyid(string id) { return dbset.find(id); } public virtual tentity getentitybyid(object[] id) { return dbset.find(id); } public virtual void insertentity(tentity entity) { dbset.add(entity); } public virtual void deleteentity(object id) { tentity entity = dbset.find(id); deleteentity(entity); } public virtual void deleteentity(tentity entity) { if(context.entry(entity).state == entitystate.detached) { dbset.attach(entity); } dbset.remove(entity); } public virtual void updateentity(tentity entity) { dbset.attach(entity); context.entry(entity).state = entitystate.modified; } }
in controller have constructor initializes unit of work , sets repository:
public studentscontroller(ifakultetrepository<students> studentrepository) { this.unitofwork = new unitofwork(); this.unitofwork.studentsrepository = studentrepository; }
and here controller method i'm testing:
public actionresult index(string sortorder, string currentfilter, string search, int? page) { viewbag.currentsort = sortorder; viewbag.namesortparm = string.isnullorempty(sortorder) ? "name_desc" : ""; viewbag.bisortparm = sortorder == "bi" ? "bi_desc" : "bi"; viewbag.citysortparm = sortorder == "city" ? "city_desc" : "city"; if (search != null) { page = 1; } else { search = currentfilter; } viewbag.currentfilter = search; var students = s in unitofwork.studentsrepository.getentities() select s; if (!string.isnullorempty(search)) { students = students.where(s => s.prezime.contains(search) || s.ime.contains(search) || s.bi.contains(search)); } switch (sortorder) { case "name_desc": students = students.orderbydescending(s => s.prezime); break; case "bi": students = students.orderby(s => s.bi); break; case "bi_desc": students = students.orderbydescending(s => s.bi); break; case "city": students = students.orderby(s => s.grad); break; case "city_desc": students = students.orderbydescending(s => s.grad); break; default: students = students.orderby(s => s.prezime); break; } int pagesize = 10; int pagenumber = (page ?? 1); return view("index", students.topagedlist(pagenumber, pagesize)); }
you creating 2 students:
students.add(new students() { bi = "10011", ime = "pera", prezime = "peric", adresa = "ulica1", grad = "grad1" }); students.add(new students() { bi = "20011", ime = "marko", prezime = "markovic", adresa = "ulica2", grad = "grad2" });
you calling controller method:
index(string sortorder, string currentfilter, string search, int? page)
with these values:
result = controller.index("", "test", "test", 1) viewresult;
in controller method apply filter results returned call getentities()
if (!string.isnullorempty(search)) { students = students.where(s => s.prezime.contains(search) || s.ime.contains(search) || s.bi.contains(search)); }
as saw above, value of search
"test"
so, assuming mock has been invoked , has returned students reason seeing no values returned in unit test
have been removed filter.
Comments
Post a Comment