【发布时间】:2014-04-08 07:52:07
【问题描述】:
这是我对调查猴子 api oauth 令牌的 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());
}
}
}
现在上面的代码工作正常,但是我在为这个方法编写单元测试时遇到问题。下面是我对它们的单元测试,我模拟我的方法返回 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 => a.VerifyRedirectedTempCode(It.IsAny())
我的Tests有什么问题。我写的测试方法是否正确。我是第一次写httpwebrequest测试方法。
【问题讨论】:
标签: c# unit-testing moq