开发者

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

开发者 https://www.devze.com 2023-02-19 20:28 出处:网络
I\'m studying how Rhino.Mocks works and trying to understand how can I set manually a value in a class Property.

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 possibl开发者_运维百科e to set manually class Properties and evaluate them to see if getters and setters are working fine?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号