0

So I have this controller that I want to test:

 [HttpPost]
        public ActionResult Login(FormCollection Form)
        {
            bool Authenticated = WebSecurity.Login(Form["UserName"], Form["Password"], false);

            if (Authenticated)
            {
                string Return_Url = Request.QueryString["ReturnUrl"];
                if (Return_Url == null)
                {
                    return Redirect("~/Igras/Index");
                }
                else
                {
                    Response.Redirect(Return_Url);
                }
            }
            return View("Login");
        }

This is my test:

 [TestMethod]
        public void testLogin()
        {

            FormCollection form = new FormCollection();
            form.Set("UserName", "renej");
            form.Set("Password", "renej");
            //form.Add("UserName", "renej");
            //form.Add("Password", "renej");
            accountCTRL = new AccountController();
            ViewResult r = accountCTRL.Login(form) as ViewResult;
            Assert.AreEqual("Login", r.ViewName);

        }

If I try to run the test, it gives me an error that the object is not set to an instance of an object(so null) on the parameter "authenticated" in the Login controller. Does anyone know how I would pass my username and password into the FormCollection so the test would give it to the controller properly?

amarnath
  • 785
  • 3
  • 19
  • 23
  • 1
    What is `WebSecurity`? I would guess that's some sort of dependency that hasn't been initialised, and thus is `null`. – Martin Costello May 12 '17 at 18:38
  • 3
    Possible duplicate of [How to unit test methods that use System.Web.Security.Membership inside?](http://stackoverflow.com/questions/12408180/how-to-unit-test-methods-that-use-system-web-security-membership-inside) – Eris May 12 '17 at 18:40
  • It's not a duplicate I've checked most topics. – averagejoex May 12 '17 at 19:07
  • I use websecurity from "SimpleMembership" to login and create roles and register users for my main site in the main MVC application. – averagejoex May 12 '17 at 19:07
  • 1
    You'd need to mock `WebSecurity` - possibly in a wrapper, and inject it with a dependency container – Alex May 12 '17 at 22:21

0 Answers0