An example of PropertyBehavior() using 3.5

237 views
Skip to first unread message

Stephen Schaff

unread,
Aug 19, 2009, 2:47:51 PM8/19/09
to Rhino.Mocks
I would really appreciate a working example of using PropertyBehavior
in Rhino Mocks 3.5.

Here is what I have (that does not work)

[TestMethod]
public void RandomTest()
{
var addressForm =
MockRepository.GenerateMock<IAddAddressForm>();

SetupResult.For(addressForm.FirstName).PropertyBehavior();
addressForm.FirstName = "Bob";
string name = addressForm.FirstName;
addressForm.AssertWasCalled(x => { var ignored =
x.FirstName; });
Assert.AreEqual(name, addressForm.FirstName);
}


This uses the following Interface:
public interface IAddAddressForm
{
string FirstName { get; set; }
}

When I run the test I get this error:
System.InvalidOperationException: Invalid call, the last call has been
used or no call has been made (make sure that you are calling a
virtual (C#) / Overridable (VB) method)..

If you know what I am doing wrong, then that would be great, but
otherwise I would just love a working example of how to make a
property of a mock use PropertyBehavior using 3.5 (no Replay/ReplayAll
please! There are Tons of examples using that syntax)

Hoping I get a response,
Stephen

Ayende Rahien

unread,
Aug 19, 2009, 2:51:10 PM8/19/09
to rhino...@googlegroups.com
Stephen, 
If you specify PropertyBehavior, it behave like a property.
That means that you can't assert on calls on it.
If you want to check that it was set, you can check the value in it.

Stephen Schaff

unread,
Aug 19, 2009, 2:56:31 PM8/19/09
to Rhino.Mocks
Ayende,

Thank you for that, it clears up some confusion.

Still the above code is failing at the SetupResult.For call (it never
even gets to the Assert call).

I would like to know how to make my mocks have the stub like
PropertyBehavior (even if I can't assert on it then).

On Aug 19, 12:51 pm, Ayende Rahien <aye...@ayende.com> wrote:
> Stephen, If you specify PropertyBehavior, it behave like a property.
> That means that you can't assert on calls on it.
> If you want to check that it was set, you can check the value in it.
>

Tim Barcz

unread,
Aug 19, 2009, 3:06:14 PM8/19/09
to rhino...@googlegroups.com
Is the property virtual?

Also have you looked at GenerateStub where PropertyBehavior is the default behavior?

Stephen Schaff

unread,
Aug 19, 2009, 3:14:25 PM8/19/09
to Rhino.Mocks
I can't make the property virtual because I am passing in an interface
(there is no way to have a virtual identifier on an interface (that I
know of)).

I have looked at the GenerateStub (and that works great). But I am
creating a presentation for my Dev team and I wanted to understand how
to make a property of a mock have PropertyBehavior.

Thanks for all the quick responses!

Stephen Schaff

unread,
Aug 19, 2009, 3:18:47 PM8/19/09
to Rhino.Mocks
I was hoping to be able to see if it was read.

I saw a demo that had a custom built mock that defined the properties
like this:

public string FirstNameText
{
get
{
FirstNameTextGetCalled = true;
return FirstNameTextGetFakeResult;
}
set
{
FirstNameTextSetCalled = true;
FirstNameTextSetLastValue = value;
}
}

I was trying to see if I could get "FirstNameTextGetCalled" like
functionality without having to write the mock myself (ie using Rhino
Mocks).

On Aug 19, 12:51 pm, Ayende Rahien <aye...@ayende.com> wrote:
> Stephen, If you specify PropertyBehavior, it behave like a property.
> That means that you can't assert on calls on it.
> If you want to check that it was set, you can check the value in it.
>

Ayende Rahien

unread,
Aug 19, 2009, 4:19:36 PM8/19/09
to rhino...@googlegroups.com
Oh, you are mixing AAA and record/replay syntax.

addressform.Stub(x=>x.FirstName);

Ayende Rahien

unread,
Aug 19, 2009, 4:29:13 PM8/19/09
to rhino...@googlegroups.com
IAddAddresForm - presumably an interface

Stephen Schaff

unread,
Aug 19, 2009, 5:05:44 PM8/19/09
to Rhino.Mocks
That was it!!!!

Thank you very much for clearing that up for me.

addressForm.Stub(x=>x.FirstName).PropertyBehavior() was exactly what I
needed.

Thanks again!

On Aug 19, 2:19 pm, Ayende Rahien <aye...@ayende.com> wrote:
> Oh, you are mixing AAA and record/replay syntax.
>
> addressform.Stub(x=>x.FirstName);
>
Reply all
Reply to author
Forward
0 new messages