Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 模拟C中同一类中的方法#_C#_Unit Testing_Nunit_Moq - Fatal编程技术网

C# 模拟C中同一类中的方法#

C# 模拟C中同一类中的方法#,c#,unit-testing,nunit,moq,C#,Unit Testing,Nunit,Moq,我的代码是这样的,我试图模拟我在GetUrlToSurveyMonkeyAuthentication方法中使用的HttPutibility urlencode方法 //this is the method which uses urlencode method which is in the same class public string GetUrlToSurveyMonkeyAuthentication(string redirectUri, string clientId

我的代码是这样的,我试图模拟我在GetUrlToSurveyMonkeyAuthentication方法中使用的HttPutibility urlencode方法

    //this is the method which uses urlencode method which is in the same class
    public string GetUrlToSurveyMonkeyAuthentication(string redirectUri, string clientId, string apiKey)
    {
        string urlToOauthSurveyMonkeyAuthentication = SurveyMonkeyBaseUrl + AuthCodeEndUrl + ParameterSeparator + ParameterRedirectUriName + UrlEncodes(redirectUri) + ParameterAdditioner + ParameterClientIdName + UrlEncodes(clientId) + ParameterAdditioner + ParameterResponseTypeName + UrlEncodes(ResponseType) + ParameterAdditioner + ParameterAPIKeyname + UrlEncodes(apiKey);
        return urlToOauthSurveyMonkeyAuthentication;
    }
    // my urlencode method which needs to be mocked it is in the same class SurveyMonkeyAPIService
    public virtual string UrlEncodes(string value)
    {
        return HttpUtility.UrlEncode(value);
    }
我的测试方法是这样的

     [Test]
    public void GetUrlToSurveyMonkeyAuthenticationTest()
    {
        var mockUrlEncodeMethod = new Moq.Mock<ISurveyMonkeyAPIService>();
        mockUrlEncodeMethod.CallBase = true;
        mockUrlEncodeMethod.Setup(x => x.UrlEncode(It.IsAny<string>())).Returns(TestData.TestData.SamplehttpUtilityURLEncodeMockString);
        string tempURL = mockUrlEncodeMethod.Object.GetUrlToSurveyMonkeyAuthentication(TestData.TestData.SampleRedirectUri, TestData.TestData.SampleClientId, TestData.TestData.SampleApiKey);
        Assert.IsNotNullOrEmpty(tempURL);
    }
[测试]
public void geturltosurveymonkeyaauthenticationtest()
{
var mockurlencodethod=new Moq.Mock();
mockUrlEncodeMethod.CallBase=true;
mockUrlEncodeMethod.Setup(x=>x.UrlEncode(It.IsAny()).Returns(TestData.TestData.SamplehttpUtilityURLEncodeMockString);
字符串tempURL=mockUrlEncodeMethod.Object.GetUrlToSurveyMonkeyAuthentication(TestData.TestData.SampleDirectUri、TestData.TestData.SampleClientId、TestData.TestData.SampleApiKey);
IsNotNullOrEmpty(tempURL);
}

我的测试失败了,它总是返回snall值,我尝试删除virtual关键字,我有一个接口,它有这两个方法定义,我没有参数构造函数。我正在使用Nunit测试这些方法

您正在模拟一个界面

var mockUrlEncodeMethod = new Moq.Mock<ISurveyMonkeyAPIService>();
var mockurlencodethod=new Moq.Mock();
您希望模拟一个,以便某些实现可用:

var mockUrlEncodeMethod = new Moq.Mock<SurveyMonkeyAPIService>();
var mockurlencodethod=new Moq.Mock();

Moq将能够模拟所述类的虚拟方法,并调用非虚拟方法的实际实现。

您正在模拟一个接口

var mockUrlEncodeMethod = new Moq.Mock<ISurveyMonkeyAPIService>();
var mockurlencodethod=new Moq.Mock();
您希望模拟一个,以便某些实现可用:

var mockUrlEncodeMethod = new Moq.Mock<SurveyMonkeyAPIService>();
var mockurlencodethod=new Moq.Mock();

Moq将能够模拟所述类的虚拟方法,并调用非虚拟方法的实际实现。

如果我尝试这样做,我将得到此异常Moq.MockVerificationException:以下设置不匹配:SurveyMonkeyAPIService x=>x.UrlEncode(It.IsAny())如果我尝试这样做,我会得到这个异常Moq.MockVerificationException:以下设置不匹配:SurveyMonkeyAppIservice x=>x.UrlEncode(It.IsAny())