Junior Mayhe
Junior Mayhe

Reputation: 16411

How to use Rhino.Mocks to evaluate class Properties (getters and setters)

I'm studying how Rhino.Mocks works and trying to understand how can I set manually a value in a class Property.

I have seen a sample in internet where you have only desired Property as argument of Expect.Call(), instead of using a method.

MockRepository mocks = new MockRepository(); 
Person p = mocks.StrictMock<Person>();
Expect.Call(p.FirstName).Return("John");

Person is a class such as:

public class Person
{
   public string FirstName {get;set;}
}

I always receive the error:

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).

Am I missing something? Is it possible to set manually class Properties and evaluate them to see if getters and setters are working fine?

Upvotes: 2

Views: 2844

Answers (1)

Paulo Santos
Paulo Santos

Reputation: 11567

As with any mocking framework, Rhino Mocks can only mock interfaces or classes that defines virtual methods and properties.

That's because when implementing a class, Rhino creates a derived class from the one you specify, replacing every virtual (or Overridable in VB) method with a stub implementation that uses an interceptor to handle the call.

When you specify a non virtual method, Rhino can't create a wrapper.

That is also true tor sealed (NonInheritable in VB) classes.

So for your class to work, you should implement the property as such:

public class Person
{
    public virtual string FirstName { get; set; }
}

This way Rhino can override the poperty accordingly.

Upvotes: 8

Related Questions