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?