2013-03-03 78 views
1

我通常不尋找一個答案,這裏的每一個問題,但更多的鏈接,學習資源,也許我的一些點建議ASP.NET MVC4代碼第一次和單元測試教程/諮詢

我米試圖實現單元測試我的項目,但我無法找到進入足夠詳細任何教程(最好是視頻教程)。

A- 1.我如何區分足夠的問題以單元測試我的功能?

A- 2.我應該更多着眼於測試,我應該在哪裏劃清界線呢?

A- 3.什麼應的一個單元測試覆蓋和在什麼時候,我應該把它分解成2個單元測試?

所以我使用代碼首先,我已經建立了一個產品模型,控制器和視圖:

// 
    // GET: /Product/Edit/5 

    public ActionResult Edit(int id = 0) 
    { 
     Product product = db.Products.Find(id); 
     if (product == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(product); 
    } 

    // 
    // POST: /Product/Edit/5 

    [HttpPost] 
    public ActionResult Edit(Product product) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Entry(product).State = EntityState.Modified; 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     return View(product); 
    } 

    [TestMethod] 
    public void TestEditView() { 
     // Arrange 
     ProductController controller = new ProductController(); 

     // Act 
     ViewResult result = controller.Edit(2) as ViewResult; 

     // Assert 
     //make sure theres a result 
     Assert.IsNotNull(result); 
     //make sure it's the right results 
     Assert.AreEqual("Edit", result.ViewName); 
     //test a dropdownlist exists 
     Assert.IsNotNull(result.ViewBag.CategoryID); 
    } 

B- 1.我應該如何測試我的POST方法?我是否應該爲數據管理添加一個額外的層,以便我可以編寫單元測試來單獨測試?

B- 2.我應該更專注於檢查返回的視圖還是更多的數據 (例如:如果我有一個Edit.cshtml,那麼它總是會返回(爲什麼測試那個?),返回的數據更重要的是,不是嗎?或者是我的黑盒子更重要嗎?或者我應該涵蓋所有3?)

B- 3.我應該打破我的單元測試來分別測試視圖和數據?

同樣,有很多的問題,但我覺得主要是我要找的例子,教程和方法來了解如何使用單元測試(甚至MVC)是最好的,我可以。

回答

2

我發現這本書臨ASP.NET MVC 4是一個很好的資源。它在使用EF的MVC項目的上下文中覆蓋單元測試做得很好。

1

我建議以 「團結,依賴注入單元測試」

更多細節

Link1

Link2

感謝 Abiruban