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); 
相关问题