Take the 2-minute tour ×

I am trying to use Moq to assign a property when a method is called.

Something along the lines of:

Mock<ITimer> mock = new Mock<ITimer>();
mock.Setup(x=>x.Start()).AssignProperty(y=>y.Enabled = true);

Is it possible to use Moq to set an expected property value when a method is called

share|improve this question
It certainly should be. I know Rhino will do this, so if Moq doesn't it would be surprising. I'm confident this is just a config method mixup or some other usage issue. But, I don't know Moq so I'll let someone more knowledgeable actually answer the question. –  KeithS Apr 10 '12 at 14:35

1 Answer 1

up vote 7 down vote accepted

I assume you are trying to essentially perform a new Setup() on your Mock when the method is called? If so, you should be able to do it with a callback, like this:

mock.Setup(x=>x.Start()).Callback(() => mock.SetupGet(y => y.Enabled).Returns(true));

It's ugly, but it should do the trick.

share|improve this answer
Thanks that did it although I went with mock.Setup(x => x.Start()).Callback(() => mock.Setup(x => x.Enabled).Returns(true)); –  Jon Apr 10 '12 at 14:45

Your Answer


