Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
        }
like image 598
Mike Flynn Avatar asked Jan 18 '26 01:01

Mike Flynn


2 Answers

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.

Instead of creating your own repository, simply use:

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

Everything should work fine now.

like image 115
PatrickSteele Avatar answered Jan 19 '26 14:01

PatrickSteele


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. 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?

like image 24
Jon Avatar answered Jan 19 '26 14:01

Jon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!