Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Moq to assign property value when method is called

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

like image 832
Jon Avatar asked Apr 10 '12 14:04

Jon


1 Answers

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<ITimer> mock = new Mock<ITimer>();
mock.Setup(x=>x.Start()).Callback(() => mock.SetupGet(y => y.Enabled).Returns(true));

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

like image 149
Marty Avatar answered Oct 22 '22 03:10

Marty