Jan 20, 2009

Create a stub with

A stub can also be created using Mocking frameworks. Semantically speaking, a stub is a simulated object, just like a mock object, that will support our test so that we can test other things. In short, a stub will always make “happy noises” when being called, and can never be used to see if the test passed or not. Calling VerifyAll() will not verify anything against stub objects. Only against mocks. Most Mocking frameworks contain the semantic notion of a stub and RhinoMocks is no exception.

        [Test] 
        public void ReturnResultsFromStub() 
        { 
            MockRepository mocks = new MockRepository(); 
            IGetRestuls resultGetter = mocks.Stub(); 
            using(repository.Record()) 
            { 
                //this is still required
                //to save the value for verification
                resultGetter.GetSomeNumber("a"); 
                LastCall.Return(1); 
                 
            } 
            //verify
            int result = resultGetter.GetSomeNumber("a"); 
            //please note here we don't use mocks.VerifyAll(); because stub
            // will never break tests, but if we use mocks.VerifyAll()
            //will not break the test anyway.
            Assert.AreEqual(1, result); 
        }