简体   繁体   English

Rhino Mocks Stub方法不起作用

[英]Rhino Mocks Stub Method not working

Why won't this test method work? 为什么这种测试方法不起作用? I keep getting requires a return value or an exception to throw. 我不断得到要求返回值或抛出异常。

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");
        }

Your repository is still in "record" mode. 您的存储库仍处于“记录”模式。 You're mixing record/replay semantics (the "old" way of doing things) with the newer AAA (arrange/act/assert) style. 您将记录/重放语义(“旧的”处理方式)与较新的AAA(编号/动作/断言)样式混合在一起。

Instead of creating your own repository, simply use: 不要创建自己的存储库,只需使用:

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

Everything should work fine now. 一切都应该现在正常。

Your last line is calling the Authenticate method on your stub object, you haven't set up a return or value or exception to throw when calling it, so Rhino Mocks doesn't know what the stub should do and it causes an error. 你的最后一行是在你的存根对象上调用Authenticate方法,你没有设置一个返回或值或异常,在调用它时抛出,所以Rhino Mocks不知道存根应该做什么,它会导致错误。 You probably don't want to call a method on your stub - that seems kind of pointless to me, is there another object (that you're actually testing in this test) that you should be calling a method on? 你可能不想在你的存根上调用一个方法 - 这对我来说似乎没有意义,是否有另一个对象(你在这个测试中实际测试)你应该调用一个方法?

Is that your whole test? 那是你的全部考试吗? If so, your test makes no sense. 如果是这样,你的测试毫无意义。 The only object in your test is the one you're stubbing--where is the subject of the test? 测试中唯一的对象就是你要抄袭的对象 - 测试的主题在哪里?

If you're trying to test the SSO class, you absolutely never want to mock/stub it. 如果你正在尝试测试SSO类,你绝对不想模拟/存根它。 If SSO has one or more dependencies, use the mocking framework to set up canned interactions between those dependencies and your SUT. 如果SSO具有一个或多个依赖项,请使用模拟框架来设置这些依赖项与您的SUT之间的固定交互。 That is the exact purpose of a mocking framework. 这就是模拟框架的确切目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM