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;
}
}
誰能告訴我,怎麼寫測試用例上述控制器的操作方法與有關使用起訂量測試用例簡要說明。
在上面寫的測試用例中有太多的編譯錯誤。預期視圖模型的視圖結果「,這意味着在斷言中,它是一個評論或我需要替換 – Amar
是的,代碼並不意味着編譯(我沒有寫在IDE中,只是在網頁上和我的到底是哪的方法來使用migth一點點關閉),而是引導你來有什麼問題的回憶。如果您正在調用控制器的Delete方法,則會返回一個'ActionResult',但在Delete的情況下,它實際上是一個ViewResult實例,因此您的測試需要驗證返回的ActionResult實際上是一個'ViewResult'實例,它具有預期的視圖模型 –