a class like this
public abstract class TT
{
public virtual int ID { get; set; }
}
public class X
{
public void F(TT t)
{
t.ID = 100;
}
}
the function X.F use TT to do something.
I what test F .
so
[TestMethod]
public void T()
{
var t = new Mock<TT>();
new X().F(t.Object);
Assert.AreEqual(100, t.Object.ID);
}
but the t.Object.ID is always “0”.
when I set ID as not virtual, it’s passed.
so, why does it? and how can i make the virtual property can be writed ?
The issue is with how Moq actually builds the mocks. Since in this case it does it by extending your class and overriding, the property marked as virtual is one that will be handled by Moq.
So I believe what is missing in your implementation is the setup for the property:
And you can also have Moq set up all your properties using:
A lot of useful examples can be found here: http://code.google.com/p/moq/wiki/QuickStart