views:

2085

answers:

3

I am stumped. I am getting this error when I try to set a mock to have "PropertyBehavior()":

System.InvalidOperationException:  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)..

I am trying to use only Rhino Mocks 3.5 (Arrange, Act Assert)

Here is my code:

    private IAddAddressForm form;

    private AddAddressMediator mediator;

    [TestInitialize()]
    public void MyTestInitialize()
    {
        form = MockRepository.GenerateMock<IAddAddressForm>();
        mediator = new AddAddressMediator(form);


        // Make the properties work like a normal property
        Expect.Call(form.OKButtonEnabled).PropertyBehavior();

        //I tried this too.  I still get the exception
        //SetupResult.For(form.OKButtonEnabled).PropertyBehavior();
    }


    [TestMethod]
    public void TestOKButtonEnabled()
    {

        form.OKButtonEnabled = true;
        Assert.IsTrue(form.OKButtonEnabled);
    }

I know I could use a Stub (and for the code above I should) but I am trying to learn Rhino Mocks.

Eventually I want to be able to make sure that several properties has their values accessed. (Any hints on how to check that form.FirstName was accessed (ie the getter was called) would also be appriecated.)

In case it is needed, here is the code to IAddressForm:

namespace AddressBook
{
    public interface IAddAddressForm
    {
        string FirstName { get; set; }
        string LastName { get; set; }
        string Address1 { get; set; }
        string State { get; set; }
        string Address2 { get; set; }
        string ZipCode { get; set; }
        string City { get; set; }
        bool OKButtonEnabled { get; set; }
    }
}

Anyway, I thought that virtual would not be a problem as I am passing in an interface, but I am clearly missing something.... Thanks in advance for any help.

A: 

I think you have to do MockRepository.ReplyAll() after you set up all expectations and before you start using this mock. So my guess in your case is that you have to move the Expect.Call line before mediator = new AddAddressMediator(form);, and stick the reply all right after that:

[TestInitialize()]
public void MyTestInitialize()
{
    form = MockRepository.GenerateMock<IAddAddressForm>();
    // Make the properties work like a normal property
    Expect.Call(form.OKButtonEnabled).PropertyBehavior();

    //I tried this too.  I still get the exception
    //SetupResult.For(form.OKButtonEnabled).PropertyBehavior();

    MockRepository.ReplyAll();
    mediator = new AddAddressMediator(form);



}
Grzenio
The exception is thrown on the Expect.Call method (before it could even get to the MockRepository.ReplayAll() method.)
Vaccano
+2  A: 

Never used PropertyBehavior before, but is this the syntax you're looking for?

form.Stub(x=>x.OKButtonEnabled).PropertyBehavior()

Rhino Mocks works completely through extension methods now. The only static call I every make anymore is to MockRepository.GenerateStub

George Mauer
That was it. Thanks
Vaccano
Extension methods *are* static.
Wim Coenen
Yes, extension methods are static. To clarify: You are encouraged to use extension methods when working with Rhino Mocks rather than traditional static calls.
George Mauer
A: 

You mentioned using a stub instead of a mock but before you go changing it I'd note that strangely, I get the Invalid Call exception when I used GenerateStub but not when I use GenerateMock.

View = MockRepository.GenerateStub<IAddressView>();
View.Stub(v => v.Message).PropertyBehavior();

This throws the Invalid call exception and yes, IAddressView.Message does have a getter and setter.

Chris Woodward
I found out from Ayende's latest version 3.6 messages that this is because stubs perform property behaviour by default.
Chris Woodward