Mike Flynn
Mike Flynn

Reputation: 24325

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

Upvotes: 3

Views: 4314

Answers (3)

Phil Sandler
Phil Sandler

Reputation: 28046

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. If SSO has one or more dependencies, use the mocking framework to set up canned interactions between those dependencies and your SUT. That is the exact purpose of a mocking framework.

Upvotes: 0

PatrickSteele
PatrickSteele

Reputation: 14697

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.

Upvotes: 3

Jon
Jon

Reputation: 16726

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?

Upvotes: 1

Related Questions