dimanche 6 septembre 2015

Unit testing mvc login

I've been creating a unit test, but seemed to failed. Can any one help me in creating a unit test for this controller?

I've already tried various ways.

    public class logInController : Controller
    {
        [HttpPost]
        public ActionResult Index(logInModel model)
        {
            if(ModelState.IsValid)
            {
                int match = 0;

                sqlConn = new SqlConnection(sqlConnString);

                sqlComm = new SqlCommand("spLogin", sqlConn);
                sqlComm.CommandType = CommandType.StoredProcedure;
                sqlComm.Parameters.AddWithValue("@userName", model.Username);
                sqlComm.Parameters.AddWithValue("@password", model.Password);

                SqlParameter userMatch = new SqlParameter("@userMatch", SqlDbType.Int);
                userMatch.Direction = ParameterDirection.Output;
                sqlComm.Parameters.Add(userMatch);

                sqlConn.Open();
                sqlComm.ExecuteNonQuery();

                match = Convert.ToInt32(sqlComm.Parameters["@userMatch"].Value);
                sqlConn.Close();

                if (match != 0)
                {
                    FormsAuthentication.SetAuthCookie(model.Username, false); 
                    return RedirectToAction("index","home");
                }
                else
                    ModelState.AddModelError("", "Invalid username or password");
            }
            return View();
        }
    }

Aucun commentaire:

Enregistrer un commentaire