Here is my implementation of a webRequest Post method for the survey monkey api oauth token.I am writing unit test for the below code.
public string GetSurveyMonkeyToken(string apiKey, string clientSecret, string tempAuthCode, string redirectUri, string clientId)
{
if (!VerifyRedirectedTempCode(tempAuthCode))//this method is in the same class which checks the temp code valid(true) or not(false)
{
return null;
}
else
{
WebRequestForTokenOfSurveyMonkey = GetWebRequestForHttpPostOfSurveyMonkeyToken(apiKey, clientSecret, tempAuthCode, redirectUri, clientId);
using (HttpWebResponse responseHttpPostForToken = GetResponse(WebRequestForTokenOfSurveyMonkey))//Getresponse method is in the same class which returns this (HttpWebResponse)webRequestObject.GetResponse()
{
string tokenJson = new StreamReader(responseHttpPostForToken.GetResponseStream()).ReadToEnd();
AccessToken accesstokenObj = JsonConvert.DeserializeObject<AccessToken>(tokenJson);
string accessTokenSurvey = accesstokenObj.access_token.ToString();
return (accesstokenObj.access_token.ToString());
}
}
}
Now the above code is working fine but I have a problem writing unit tests to this method.below are my unit test to them,One test where I mock my method to return false works fine it returns null.
[Test]
public void GetSurveyMonkeyTokenTestWithValidTempCode()
{
var mockedSurveyMonkeyToken = new Moq.Mock<SurveyMonkeyAPIService>();
mockedSurveyMonkeyToken.CallBase = true;
mockedSurveyMonkeyToken.Setup(a => a.VerifyRedirectedTempCode(It.IsAny<string>())).Returns(true);
var mockRequest = mockedSurveyMonkeyToken.Object.GetWebRequestForHttpPostOfSurveyMonkeyToken(TestData.TestData.SampleApiKey, TestData.TestData.SampleClientSecret, TestData.TestData.SampleTempAuthCode, TestData.TestData.SampleRedirectUri, TestData.TestData.SampleClientId);
mockedSurveyMonkeyToken.VerifyAll();
}
The error for this test method is Moq.MockVerificationException : The following setups were not matched: SurveyMonkeyAPIService a => a.VerifyRedirectedTempCode(It.IsAny())
What is the problem in my Tests.Did I write the test methods properly.I am writing the httpwebrequest test methods for the first time.