0

I create an Nunit test class for my project, the code is shown below, the unit test class is under the above code. It shows message Null Reference Exception. I want to test but I don't know how to fix it, could you guy help me?

public class LoginController : ControllerBase
    {
private ILoginService _loginService;

        public LoginController(ILoginService login)
        {
            _loginService = login;
        }
[Authorize]
        [HttpPost("Post")]
        public string Post()
        {
            var identity = HttpContext.User.Identity as ClaimsIdentity;
            IList<Claim> claim = identity.Claims.ToList();
            var phoneNumber = claim[0].Value;
            return "Welcome To: " + phoneNumber;
        }
}

The Test class is :

public ILoginService _loginService;

public async Task Test_Post()
        {                                 
                var phoneNumberCheck = "0399838239";
                var expect = "Welcome To: " + phoneNumberCheck;
          
                var login_controller = new LoginController(_loginService);


                var actionResult = login_controller.Post();
            
                Assert.AreEqual(actionResult, expect);
            
            }

It shows a message System. NullReferenceException, how can I fix this?

Vickarius
  • 17
  • 3
  • In particular, what are you expecting to assign `_loginService` a non-null reference in your test code? – Jon Skeet Mar 13 '22 at 10:11

0 Answers0