Alex Rivera | Logout

Mocking virtual readonly properties with moq

Asked 2009-09-21T12:02:45.683
60

I couldn't find a way to do this, though this can be done by hand so why not with moq?

Edit
Report

1 Answer

101

Given this class

public abstract class MyAbstraction
{
    public virtual string Foo
    {
        get { return "foo"; }
    }
}

you can set up Foo (a read-only property) like this:

var stub = new Mock<MyAbstraction>();
stub.SetupGet(x => x.Foo).Returns("bar");

stub.Object.Foo will now return "bar" instead of "foo".

answered 2009-09-21T12:19:55.050

Your Answer