我使用moq框架來測試我的MVC應用程序。我添加了一個通用的存儲庫和工作單元類。當我運行我的控制器的測試時,它失敗了,因爲即使我添加了兩個元素,模擬模塊也會返回一個空列表(大小爲0)。下面是我的測試:ASP.NET MVC moq返回空列表
[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) as ViewResult;
var listResult = result.ViewData.Model as PagedList<Students>;
var list = listResult.ToList();
Assert.AreEqual(2, list.Count);
}
這是我的一個實現我的通用倉庫接口通用倉庫:
public class FakultetRepository<TEntity> : IFakultetRepository<TEntity> where 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;
}
}
在控制器我有初始化工作的單位和構造設置其倉庫:
public StudentsController(IFakultetRepository<Students> studentRepository)
{
this.unitOfWork = new UnitOfWork();
this.unitOfWork.StudentsRepository = studentRepository;
}
這裏是控制器方法我測試:
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 = from 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));
}
很可能'students.ToPagedList(1,5)'返回您觀察者的空列表。我的猜測是,你正在返回第5頁,其中頁面大小爲1,並且有兩個元素,第5頁上沒有任何內容可以顯示。 –