2015-07-21 72 views
1

我使用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)); 
     } 
+1

很可能'students.ToPagedList(1,5)'返回您觀察者的空列表。我的猜測是,你正在返回第5頁,其中頁面大小爲1,並且有兩個元素,第5頁上沒有任何內容可以顯示。 –

回答

2

你正在創建兩個學生:

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" 
}); 

要調用這個控制器的方法:

Index(string sortOrder, string currentFilter, string search, int? page) 

有了這些值:

result = controller.Index("", "test", "test", 1) as ViewResult; 

在應用過濾器控制方法從呼叫返回結果到GetEntities()

if (!String.IsNullOrEmpty(search)) 
{ 
     students = students.Where(s => s.Prezime.Contains(search) 
            || s.Ime.Contains(search) 
            || s.BI.Contains(search)); 
}  

正如我們上面看到的search值是「測試」

因此,假設模擬已被調用和返回的學生,你看到沒有在你的單位返回的值測試

的原因

它們已被過濾器刪除。

+0

哦,我一直在應用搜索篩選器,刪除它並且工作正常。簡直不敢相信我錯過了,多麼愚蠢的錯誤,現在需要休息一下。 :) 謝謝! – somi

+0

沒問題 - 我一直想使用擾流標籤的年齡:) – stuartd

0

也使用IList而不是列表它將做的伎倆。

請注意,您必須在視圖中顯示屬性。

[Required] 
    public IList<ListProductViewModel> Products { get; set; } 

@for (int i = 0; i < Model.Products.Count(); i++) 
    { 
    <tr [email protected][i].Id> 
    <td>@(rowNo++).</td> 
    <td>@Html.DisplayFor(modelItem => Model.Products[i].Name)</td> 
    <td>@Html.DisplayFor(modelItem => Model.Products[i].Description)</td> 
    <td>@Html.DisplayFor(modelItem => Model.Products[i].Price)</td> 
    <td>@Html.DisplayFor(modelItem => Model.Products[i].Tax)</td> 
    <td>@Html.DisplayFor(modelItem => Model.Products[i].MeasureUnit)</td> 
    <td>@Html.DisplayFor(modelItem => Model.Products[i].Currency)</td> 
    <td>@Html.NumberTextBoxFor(modelItem => Model.Products[i].Quantity)</td> 
    <td>@Html.CheckBoxBoxFor(modelItem => Model.Products[i].IsSelected)</td> 
    </tr> 
    } 
+0

複製粘貼我的代碼將無法正常工作,因爲我使用了一些HTML助手,擴展了一些經典的MVC助手 –