2

I got below Error.

" An exception of type 'System.NullReferenceException' occurred in Effort Estimation.dll but was not handled in user code Additional information: Object reference not set to an instance of an object. "

In controller I have a method that name Login. In that method we use session. So how to write unit testing? While run the unit test code, It goes to main csharp code and enter into the method and stopped in session declarations. Any one if know the solution solve it soon as possible.

My unit test Code, What I have tried is, LoginModel class have 2 variables I have assigned there.

[TestMethod]
public void Login1() //Bug
{
    LoginController log = new LoginController();
    LoginModel logModel = new LoginModel ();
    logModel.UserName = "20079199";
    logModel.Password = "123456";
    var result = log.Login(logModel) as ViewResult;
    Assert.AreEqual("HomePage", result);

}

[HttpPost]
public ActionResult Login(LoginModel lVM)
{
    LoginViewModel model = UM.Authentication(lVM);
    if (model.UserId > 0)
    {
        Session["LoginMessage"] =  model.UserName;
        Session["UserID"] = model.UserId;

        IDataHelper ex = new DataHelper();

        return RedirectToAction("HomePage", "Newcontroller");
    }
    else
    {
        Session["LoginMessage"] = "Invalid UserName and Password";
        return RedirectToAction("Login", "Login");
    }
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Param
  • 77
  • 1
  • 2
  • 9
  • 5
    http://stackoverflow.com/questions/9624242/setting-httpcontext-current-session-in-a-unit-test – Eddie Paz Jul 25 '16 at 10:25
  • I would say: why do you want to test this method? I personally wouldn't bother. Still, the link by Eddie Paz answers to your question. – Alberto Chiesa Jul 25 '16 at 12:52
  • @A.Chiesa - My TL told me to unit test to all the methods in the Controller. While execute the Unit test that show some error at method in the controller. that is " Null reference Exception. Object reference not set an instance of an object " – Param Jul 25 '16 at 13:01
  • If someone is willing to pay for a useless test, then go for it! The proper answer to your question is in the comment by Eddie Paz. If you don't know what a NullReferenceException is, then you'll have to study a bit. You can start here: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Alberto Chiesa Jul 25 '16 at 13:03
  • and: result is going to be null no matter what: log.Login(logModel) as ViewResult yields null. Because it's not returning a ViewResult, but a RedirectToAction. – Alberto Chiesa Jul 25 '16 at 13:07

1 Answers1

2

I think you have three choices here:

1) Use the HttpContextBase and mock out what you need to in the controller

2) Create a testable LoginController which inherits from LoginController e.g.

public class TestAbleLoginController : LoginController{}

Once you do this wrap the code which sets the session in a new method and then override that method in the testable controller so it doesn't set the session if you don't need it.

e.g.

protected internal override SetLoginInfo(){ //do nothing}

While the actual code does

protected virtual SetLoginInfo(){ Session["LoginMessage"]...}

3) Wrap the code to access the session in another object and inject this in via the constructor. So you could define an interface like IUserSession with what you need then just mock this interface in your test so it doesn't do anything like

var userSession = MockRepository.GenerateStub<IUserSession>();
Michael Ciba
  • 561
  • 3
  • 6