0

我確實爲以下方法生成了測試用例;它返回Action結果 - 一個json「Success」字符串。如何修改生成的腳本?寫單位返回行動結果的方法的測試用例

public ActionResult LoginClick(string username, string password) 
    { 
     LoginRepo loginrepo = new LoginRepo(); 
     BL bl = new BL(); 

     bool loginStatus = loginrepo.CheckLogin(username, password); 
     if (loginStatus) 
     { 
      bl.SetSessionVariable("Username", username); 
      return Json(new { Status = "Success" }, JsonRequestBehavior.AllowGet); 
     } 
     return Json(new { Status = "Failed" }, JsonRequestBehavior.AllowGet); 
    } 

以下是生成的測試用例。

[TestMethod()] 
     [HostType("ASP.NET")] 
     [UrlToTest("http://localhost:1280")] 
     public void LoginClickTest() 
     { 
      LoginController target = new LoginController(); // TODO: Initialize to an appropriate value 
      string username = null; // TODO: Initialize to an appropriate value 
      string password = null; // TODO: Initialize to an appropriate value 
      ActionResult expected = null ; // TODO: Initialize to an appropriate value 
      ActionResult actual; 
      actual = target.LoginClick(username, password); 
      Assert.AreEqual(expected, actual); 
      Assert.Inconclusive("Verify the correctness of this test method."); 

     } 

我該如何修改它以便它可以成功運行?既然它的行爲結果和數據是json字符串應該預期什麼,實際結果?我應該如何寫assert?

+0

他回答有幫助嗎? – SBirthare 2014-10-31 06:13:59

回答

0
How should i modify it so that it can run successfully? 

您將需要注入LoginRepo。與例如here

自動作結果和數據JSON字符串應該怎樣 預期,實際結果呢?我應該怎麼寫斷言更多信息?

您需要執行投像一個類型,因此從得到的ActionResult JsonResult:

var viewResult = yourTargetController.LoginClickTest() as JsonResult; 
    Assert.AreEqual(false, viewResult.Data); 
相關問題