這是我爲調查猴子api oauth token的webRequest Post方法的實現。我正在爲以下代碼編寫單元測試。嘲笑同一類中的方法
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());
}
}
}
現在上面的代碼工作正常,但我有一個問題編寫單元測試這個method.below是我的單元測試對他們來說,一個測試,讓我嘲笑我的方法返回false工作正常返回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();
}
此測試方法中的誤差是 Moq.MockVerificationException:下面設置不匹配: SurveyMonkeyAPIService一個=> a.VerifyRedirectedTempCode(It.IsAny())
中有什麼問題我的Tests.Did正確地編寫測試方法。我正在第一次編寫httpwebrequest測試方法。
非常感謝 – user3324848