Setting mock property via Setup causes 'Expression is not a method invocation'

I think you should be returning the mock you've created in the first part for the second part:

var mockRecorder = new Moq.Mock<Recorder>();
mock.Setup(x => x.Recorder).Returns(mockRecorder.Object);
mockRecorder.Setup(x => x.RunState).Returns(Recorder.eRunStates.Play);

That's just a guess, without having used Moq myself - but it makes sense.

However, this looks like it's going to end up being fairly fragile. You might want to consider using a fake instead here - for at least one of the objects, if not both.

EDIT: Looking at the documentation, an alternative would be:

// Moq will set up the hierarchy for you...
mock.Setup(x => x.Recorder.RunState).Returns(Recorder.eRunStates.Play);

I have found that creating a mock of Recorder and then assigning values to the mock object seems to fix the issue. Not sure if that is the correct way to do things though.

var mockRecorder = new Moq.Mock<Recorder>();
mockRecorder.Object.RunState = Recorder.eRunStates.Play;

Tags:

C#

.Net

Mocking

Moq