Wednesday, 18 July 2012

Previous method "method name" requires a return value or an exception to throw

Today i came across this error, i took me a while to figure out the solution. following is an example


[TestFixture]
    public class RhinoTest
    {
        public interface ICompany
        {
            IPerson Contact { get; }
        }

        public interface IPerson
        {
            string Name { get; }
            string Address { get; }
        }

        [Test]
        public void RhinoMockCanStubMultipleLevels()
        {
            var company = MockRepository.GenerateMock();
            company.Stub(x => x.Contact.Name).Return("contact name");
            company.Stub(x => x.Contact.Address).Return("address");
            Assert.That(company.Contact.Name, Is.EqualTo("contact name"));
        }       
    }

 Above gave me error "Previous method "method name" requires a return value or an exception to throw" on Red line. Following was my solution, i had to Mock Contact separately.

[TestFixture]
    public class RhinoTest
    {
        public interface ICompany
        {
            IPerson Contact { get; }
        }

        public interface IPerson
        {
            string Name { get; }
            string Address { get; }
        }

        [Test]
        public void RhinoMockCanStubMultipleLevels()
        {
            var company = MockRepository.GenerateMock();
            var contact = MockRepository.GenerateMock();
            contact.Stub(x => x.Name).Return("contact name");
            contact.Stub(x => x.Address).Return("address");
            Assert.That(company.Contact.Name, Is.EqualTo("contact name"));
        }       
    }