2015-10-29 91 views
2

我最近開始學習單元測試MVC和使用NUnit的框架測試用例。我的問題是,我不明白我應該寫什麼測試用例。想象一下,我有CRUD操作,我想測試它們,那麼我的測試用例條件應該如何。NUnit的測試與起訂量

這裏是我的接口類:

public interface IUserRepository 
{ 
    //Creating Single User Records into database using EF. 
    bool CreateUser(tbl_Users objUser); 

    //Updating Single User Records into database using EF. 
    void UpdateUser(tbl_Users objUser); 

    //Deleting Single User Records from database using EF. 
    bool DeleteUser(long IdUser); 
} 

這裏是我的倉儲類:

public class UserRepository : IUserRepository 
{ 
    DBContext objDBContext = new DBContext();  

    /// <summary> 
    /// Creating new User Record into Database 
    /// </summary> 
    /// <param name="objUser"></param> 
    public bool CreateUser(tbl_Users objUser) 
    { 
     bool blnResult = false; 
     objUser.MiddleName = string.IsNullOrEmpty(objUser.MiddleName) ? string.Empty : objUser.MiddleName.Trim(); 
     objUser.Photo = string.Empty; 
     objUser.Approved = false; 
     objUser.CreatedDate = DateTime.Now; 
     objUser.DeleteFlag = false; 
     objUser.UpdBy = 0; 
     objUser.UpdDate = DateTime.Now; 
     objDBContext.tbl_Users.Add(objUser); 
     blnResult = Convert.ToBoolean(objDBContext.SaveChanges()); 
     return blnResult; 
    } 

    /// <summary> 
    /// Updating existing User Record into Database 
    /// </summary> 
    /// <param name="objUser"></param> 
    public void UpdateUser(tbl_Users objUser) 
    { 
     objUser.MiddleName = string.IsNullOrEmpty(objUser.MiddleName) ? string.Empty : objUser.MiddleName.Trim(); 
     objUser.Approved = true; 
     objUser.UpdBy = objUser.IdUser; 
     objUser.UpdDate = DateTime.Now; 
     objDBContext.Entry(objUser).State = EntityState.Modified; 
     objDBContext.SaveChanges(); 
    } 

    /// <summary> 
    /// Deleting existing User Record from Database 
    /// </summary> 
    /// <param name="IdUser"></param> 
    public bool DeleteUser(long IdUser) 
    { 
     bool blnResult = false; 
     tbl_Users objUser = objDBContext.tbl_Users.Where(x => x.IdUser == IdUser).Single(); 
     objUser.ConfirmPassword = objUser.Password; 
     objUser.UpdDate = DateTime.Now; 
     objUser.DeleteFlag = true; 
     blnResult = Convert.ToBoolean(objDBContext.SaveChanges()); 
     return blnResult; 
    }   
} 

,這裏是我的控制器類

public class UserController : Controller 
{ 
    tbl_Users objUser = new tbl_Users(); 
    UserRepository Repository = new UserRepository();   

    [ValidateAntiForgeryToken] 
    [HttpPost] 
    public ActionResult Create(tbl_Users objUser) 
    { 
     if (ModelState.IsValid) 
     { 
      try 
      { 
       Repository.CreateUser(objUser); 
       return RedirectToAction("Update", "User"); 
      } 
      catch 
      { 
       return View(); 
      } 
     } 
     return View(); 
    }  

    [ValidateAntiForgeryToken] 
    [HttpPost] 
    public ActionResult Update(tbl_Users objUser) 
    { 
     Repository.UpdateUser(objUser); 
     return View(); 
    } 

    public ActionResult Delete(long IdUser = 0) 
    { 
     bool blnResult = Repository.DeleteUser(IdUser); 
     if (blnResult) 
     { 
      return View("Delete"); 
     } 
     else 
     { 
      return View(); 
     } 
    } 
} 

下面是測試案例,我試着以執行使用起訂量

[TestFixture] 
public class UserControllerTest 
{ 
    UserController Controller; 

    [SetUp] 
    public void Initialise() 
    { 
     Controller = new UserController(); 
    }  

    [Test] 
    public void DeleteTest() 
    { 
     var ObjUser = new Mock<IUserRepository>(); 

     ObjUser.Setup(X => X.DeleteUser(It.IsAny<long>())).Returns(true); 

     var Result = ObjUser.Object.DeleteUser(1); 
     Assert.That(Result, Is.True); 
    }   

    [Test] 
    public void CreateTest() 
    { 
     tbl_Users User = new tbl_Users(); 
     Mock<IUserRepository> MockIUserRepository = new Mock<IUserRepository>(); 
     MockIUserRepository.Setup(X => X.CreateUser(It.IsAny<tbl_Users>())).Returns(true); 

     var Result = MockIUserRepository.Object.CreateUser(User); 

     Assert.That(Result, Is.True); 
    }   

    [TearDown] 
    public void DeInitialise() 
    { 
     Controller = null; 
    } 
} 

誰能告訴我,怎麼寫測試用例上述控制器的操作方法與有關使用起訂量測試用例簡要說明。

回答

3

你有一對夫婦的問題。首先是你沒有測試過你的控制器,你已經測試了你的模擬器。第二個是你的控制器創建它自己的用戶存儲庫。這意味着即使您正在測試它,您也無法提供模擬用戶存儲庫來測試。

第一個解決方案是通過調用其方法並聲明結果來測試控制器,但是在測試中可以完成之前,您將解決第二個問題。

要解決第二個問題,您需要應用dependency inversion principal並將您的IUserRepository實施傳遞到您的控制器(通過理想的構造函數)。

你可以改變你的控制器有這樣的構造:

public class UserController : Controller 
{ 
    tbl_Users objUser = new tbl_Users(); 
    IUserRepository Repository; 

    public UserController(IUserRepository userRepository) 
    { 
     Repository = userRepository; 
    } 

    ...etc  
} 

那麼你可以改變你的測試更是這樣的:

[TestFixture] 
public class UserControllerTest 
{ 
    [Test] 
    public void DeleteTest() 
    { 
     var ObjUser = new Mock<IUserRepository>(); 

     ObjUser.Setup(X => X.DeleteUser(It.IsAny<long>())).Returns(true); 

     var Result = new UserController(ObjUser.Object).Delete(1); 
     Assert.That(Result, //is expected view result with expected model); 
     Assert.That(ObjUser.Verify(), Is.True);   
    }   

    [Test] 
    public void CreateTest() 
    { 
     tbl_Users User = new tbl_Users(); 
     Mock<IUserRepository> MockIUserRepository = new Mock<IUserRepository>(); 
     MockIUserRepository.Setup(X => X.CreateUser(It.IsAny<tbl_Users>())).Returns(true); 

     var Result = var Result = new UserController(ObjUser.Object).Create(User);; 

     Assert.That(Result, //is a view result with expected view model); 
     Assert.That(ObjUser.Verify(), Is.True); 
    }   
} 

現在正在測試的實際控制人檢查它返回正確的視圖,並以預期的方式與模擬存儲庫交互

+0

在上面寫的測試用例中有太多的編譯錯誤。預期視圖模型的視圖結果「,這意味着在斷言中,它是一個評論或我需要替換 – Amar

+0

是的,代碼並不意味着編譯(我沒有寫在IDE中,只是在網頁上和我的到底是哪的方法來使用migth一點點關閉),而是引導你來有什麼問題的回憶。如果您正在調用控制器的Delete方法,則會返回一個'ActionResult',但在Delete的情況下,它實際上是一個ViewResult實例,因此您的測試需要驗證返回的ActionResult實際上是一個'ViewResult'實例,它具有預期的視圖模型 –