I have written a unit test for a method so every thing is working properly but there is only one issue with one mock object ,that object is always returning null value
Mocking of Get Token Method always returning null
What I have tried:
this is the unit test which I have written in this everything is working properly except GetToken method after mocking this method always returning null value
public void UserRegistrationUnitTest()
{
RegistrationDTO inputParameters = new RegistrationDTO()
{
CountryCode="IN",
Email="abc.abc@abc.com",
FirstName="Abc",
LastName="Abc",
IsChild=0,
Password="abc@12345",
ReuseEmailAddress=0,
TermsofUseAgreed=Confirm.Yes,
UserName="abc123"
};
CountryList expectedOutputCountryData = new CountryList()
{
en=new List<En>()
{
new En { name="AF",value="Afghanistan"},
new En { name="AX",value="Åland Islands"},
new En { name="AL",value="Albania"},
new En { name="DZ",value="Algeria"},
new En { name="IN",value="India"}
}
};
RegistrationSuccessDTO DTOOutput = new RegistrationSuccessDTO() { ApprovalNeeded =false, Communication = "" };
HttpResponseMessage expectedOutputUserReg = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(JsonConvert.SerializeObject(DTOOutput))
};
LoginAuthDTO objInput = new LoginAuthDTO() { Username = inputParameters.UserName, Password = inputParameters.Password };
TokenDTO objOuptput = new TokenDTO()
{
AccessToken = CommonUtility.AccessToken
};
HttpResponseMessage fout = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content =new StringContent(JsonConvert.SerializeObject(objOuptput))
};
string serilizeCountrylst = JsonConvert.SerializeObject(expectedOutputCountryData);
objMockIJsonReadDeserializeUtility.Setup(x => x.ReadJsonandDeserialize<CountryList>("Data/Countries.json")).Returns(expectedOutputCountryData);
objMockIRegistrationMEEService.Setup(x => x.UserRegistration(inputParameters)).Returns(Task.FromResult(expectedOutputUserReg));
objMockITokenApplicationService.Setup(x => x.GetToken(objInput)).Returns(Task.FromResult(fout));
HttpResponseMessage finalOutput=objRegistrationService.UserRegistration(inputParameters).Result;
string finalcontent = finalOutput.Content.ReadAsStringAsync().Result;
CountryList contriesOutPut = objMockIJsonReadDeserializeUtility.Object.ReadJsonandDeserialize<CountryList>("Data/Countries.json");
RegistrationSuccessDTO registrationOutput = JsonConvert.DeserializeObject<RegistrationSuccessDTO>(((StringContent)objMockIRegistrationMEEService.Object.UserRegistration(inputParameters).Result.Content).ReadAsStringAsync().Result);
Assert.NotNull(registrationOutput);
Assert.NotNull(contriesOutPut);
Assert.NotNull(finalcontent);
}
Actulal Method
public async Task<HttpResponseMessage> UserRegistration(RegistrationDTO registrationDTO)
{
CountryList lstCountryList = _ijsonReadDeserializeUtility.ReadJsonandDeserialize<CountryList>("Data/Countries.json");
var checkcountry = lstCountryList.en.Any(x=>x.name == registrationDTO.CountryCode);
if (checkcountry)
{
if (Regex.IsMatch(registrationDTO.Email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase))
{
if (registrationDTO.TermsofUseAgreed == Confirm.Yes)
{
var response = await _registrationMEEService.UserRegistration(registrationDTO);
string strContentString = await response.Content.ReadAsStringAsync();
if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
{
return response;
}
else if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
RegistrationSuccessDTO responseobject = JsonConvert.DeserializeObject<RegistrationSuccessDTO>(strContentString);
if (!responseobject.ApprovalNeeded)
{
LoginAuthDTO authParams = new LoginAuthDTO() { Username = registrationDTO.UserName, Password = registrationDTO.Password };
var tokenResponse = await _tokenApplicationService.GetToken(authParams);
if (tokenResponse != null && tokenResponse.IsSuccessStatusCode)
{
string contentString = await tokenResponse.Content.ReadAsStringAsync();
TokenDTO tokenDTO = JsonConvert.DeserializeObject<TokenDTO>(contentString);
List<object> lstTokenRegistration = new List<object>();
lstTokenRegistration.Add(tokenDTO);
lstTokenRegistration.Add(responseobject);
return new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(JsonConvert.SerializeObject(lstTokenRegistration),
Encoding.UTF8, "application/json")
};
}
else
{
return new HttpResponseMessage()
{
StatusCode = HttpStatusCode.Unauthorized,
Content = new StringContent(JsonConvert.SerializeObject(new MEEError() { Error = "UnAuthorised User", ErrorDescription = "User UnAuthorised" }),
Encoding.UTF8, "application/json")
};
}
}
else
{
return new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(JsonConvert.SerializeObject(responseobject),
Encoding.UTF8, "application/json")
};
}
}
else
{
return response;
}
}
else
{
return new HttpResponseMessage()
{
StatusCode = HttpStatusCode.BadRequest,
Content = new StringContent(JsonConvert.SerializeObject(new MEEError() { Error = "AgreedTerms&ConditionError", ErrorDescription = "Please check Terms and Condition" }),
Encoding.UTF8, "application/json")
};
}
}
else
{
return new HttpResponseMessage()
{
StatusCode = HttpStatusCode.BadRequest,
Content = new StringContent(JsonConvert.SerializeObject(new MEEError() { Error = "InValidMailFormat", ErrorDescription = "Enter Valid Email" }),
Encoding.UTF8, "application/json")
};
}
}
else
{
return new HttpResponseMessage()
{
StatusCode =HttpStatusCode.BadRequest,
Content = new StringContent(JsonConvert.SerializeObject(new MEEError() { Error = "InvalidCountryCode", ErrorDescription ="Enter Valid Country Code" }),
Encoding.UTF8, "application/json")
};
}
}