Stubs are used to provide "canned" responses. You usually don't set expectations on them nor do anything "special" -- they just exist to provide functionality for some other component that you don't want as part of your unit test. Therefore, the default behavior for properties on Stubs is to just accept whatever value is set at runtime and return that value when requested.
Since you want specific behavior (an exception to be thrown), I would go with a Mock (which, as you pointed out, throws the exception as you desired). --- Patrick Steele http://weblogs.asp.net/psteele On Wed, Mar 17, 2010 at 10:42 AM, [email protected] <[email protected]> wrote: > I have the following code: > public interface ILala > { > int AAA { get; set; } > } > public class BBB > { > public int Meth(ILala l) > { > return l.AAA; > } > } > and I'm trying to run the following test : > [Test] > [ExpectedException(typeof(ApplicationException))] > public void RhinoMockProperty() > { > // > // Arrange > BBB instance = new BBB(); > ILala lStub = > MockRepository.GenerateStub<ILala>(); > lStub.Stub(x => x.AAA).Throw(new ApplicationException()); > // > // Act > instance.Meth(lStub); > } > > this code fails with the following exception : > System.InvalidOperationException : You are trying to set an > expectation on a property that was defined to use PropertyBehavior. > Instead of writing code such as this: mockObject.Stub(x => > x.SomeProperty).Return(42); > You can use the property directly to achieve the same result: > mockObject.SomeProperty = 42; > at Rhino.Mocks.Impl.RecordMockState.GetLastMethodOptions[T]() > at Rhino.Mocks.MockRepository.LastMethodCall[T](Object > mockedInstance) > at Rhino.Mocks.LastCall.GetOptions[T]() > at Rhino.Mocks.RhinoMocksExtensions.Expect[T,R](T mock, Function`2 > action) > at Rhino.Mocks.RhinoMocksExtensions.Stub[T,R](T mock, Function`2 > action) > BBBTests.cs(19,0): at > ClassLibrary14.Tests.BBBTests.RhinoMockProperty() > > > I tried doing the same with GenerateMock instead of GenerateStub and > it works. > Is there any other workaround ? -- You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rhinomocks?hl=en.
