tags:

views:

365

answers:

3

Any idea in Moq for a class with readonly modifier :

Class myClass
{
   private  int id;
   public int Id{ get {return id;}}

  public myClass(int id)
  { this.id  = id }
}

I was trying to mock this object:

var myMock= new Mock<myClass>();
myMock.SetupGet(m => m.ID).Return(555);

It returned me error:

System.ArgumentException: Invalid setup on a non-overridable member m=>m.ID.

Any idea?

+3  A: 

The problem is not that it is readonly, but that it is not virtual.

Nat
A: 

I don't know Moq, but is there a chance that it's actually complaining that the property isn't virtual?

Jonathan
+1  A: 

The only mocking engine I know of that allows altering non-virtual methods on classes and sealed classes is Typemock.

Most of the other mocking frameworks work better with interfaces, and if an interface isn't available the members being mocked need to be virtual.

KeeperOfTheSoul