这是我为调查猴子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