Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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

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# Rhino Mocks存根方法不起作用_C#_Unit Testing_Rhino Mocks_Stub - Fatal编程技术网

C# Rhino Mocks存根方法不起作用

C# Rhino Mocks存根方法不起作用,c#,unit-testing,rhino-mocks,stub,C#,Unit Testing,Rhino Mocks,Stub,为什么这种测试方法不起作用?我不断获取需要返回值或抛出异常 public AuthenticateResponse Authenticate(string username, string password) { string response = GetResponse(GetUrl(username, password).ToString()); return ParseResponse(response);

为什么这种测试方法不起作用?我不断获取需要返回值或抛出异常

public AuthenticateResponse Authenticate(string username, string password)
        {
            string response = GetResponse(GetUrl(username, password).ToString());

            return ParseResponse(response);
        }


        [TestMethod()]
        [ExpectedException(typeof(XmlException))]
        public void Authenticate_BadXml_ReturnException()
        {
            MockRepository mockRepository = new MockRepository();
            SSO sso = mockRepository.Stub<SSO>();

            sso.Stub(t => t.GetResponse("")).Return("<test>d");

            AuthenticateResponse response = sso.Authenticate("test", "test");
        }
公共身份验证响应身份验证(字符串用户名、字符串密码)
{
string response=GetResponse(GetUrl(用户名、密码).ToString();
返回解析响应(response);
}
[TestMethod()]
[ExpectedException(typeof(XmlException))]
public void Authenticate_BadXml_ReturnException()
{
MockRepository MockRepository=新建MockRepository();
SSO=mockRepository.Stub();
sso.Stub(t=>t.GetResponse(“”).Return(“d”);
AuthenticateResponse响应=sso.Authenticate(“测试”、“测试”);
}

您的最后一行是在存根对象上调用
Authenticate
方法,您没有设置调用它时要抛出的返回、值或异常,因此Rhino Mocks不知道存根应该做什么,它会导致错误。您可能不想在存根上调用方法—这对我来说似乎毫无意义,您是否应该在另一个对象(您在本测试中实际测试的对象)上调用方法?

您的存储库仍处于“记录”模式。您将录制/重播语义(旧的处理方式)与新的AAA(arrange/act/assert)样式混合在一起

不要创建自己的存储库,只需使用:

var sso = MockRepository.GeneateStub<SSO>();
var sso=MockRepository.geneatsub();

现在一切正常。

这就是你的全部测试吗?如果是这样,你的测试就没有意义了。测试中唯一的对象是您正在存根的对象--测试的主题在哪里


如果您试图测试SSO类,您绝对不想模拟/存根它。如果SSO有一个或多个依赖项,请使用模拟框架在这些依赖项和SUT之间设置固定的交互。这正是模拟框架的目的。

GetResponse方法是虚拟的吗?我想我用错了。authenticate方法调用外部服务并获取数据。我想用d来模拟它,并将它发送到同样在authenticate中的Parse方法。这修复了该错误,但现在它没有命中Parse方法,而是模拟GetResponse方法。我在上面添加了实际方法。它是否覆盖了Parse方法?返回ParseResponse(response);啊,好的。因此,看起来您想要做的是部分模拟(因为您想要删除类的某些部分,但在另一部分中运行真正的代码)。我已经有一段时间没有在Rhino.Mocks中这样做了,但是您需要使用PartialMock,确保GetResponse()是虚拟的,并确保GetUrl(用户名、密码)返回空字符串(这是您定义存根调用的方式)。或者,如果您不关心参数,请在存根调用后使用.IgnoreArguments()。我想我应该直接测试parse方法,而不是authenticate方法本身。我的依赖项是我试图删除的HTTP请求。