SetupSet on hierarchy
I want to do something like following which is not allowed
myMockedObject.SetupSet(x => x.SomeMethodReturningCustomer().Age = 18);
It returns
Expression is not a property setter invocation.
When mock.Setup can setup a hierarchy then why is SetupSet limited strictly to direct property assignment? Same applies to SetupGet
I have the same issue with SetupSet. The following code throws Expression is not a property setter invocation.
private string _testText;
public virtual string TestText
{
get
{
return _testText;
}
set
{
if (CheckPropertyChanged<string>("TestText", ref _testText, ref value))
{
FirePropertyChanged("TestText");
}
}
}
[Test]
public void TestText_ReturnsSetValue()
{
_viewModelMock.SetupSet(x => x.TestText = "1");
Assert.AreEqual("1", _viewModelMock.Object.TestText);
}
Is there a way to set up a complex setter on a mock?
@AbhishekTripathi:
myMockedObject.SetupSet(x => x.SomeMethodReturningCustomer().Age = 18);
The above will work just fine as long as the Age property in question is declared virtual. This will be automatically the case if your customer type is an interface type; if it's a class, you need to explicitly make Age virtual so that Moq can mock / intercept it.
@desperate-man:
_viewModelMock.SetupSet(x => x.TestText = "1"); Assert.AreEqual("1", _viewModelMock.Object.TestText);
It appears that you have a misconception about how SetupSet works. SetupSet as you have used to sets up an expectation that someone will set TestText to "1". It does not set up TestText to return the value "1". You would do that like this:
_viewModelMock.SetupGet(x => x.TestText).Returns("1"); // or just `Setup` will also work
See the "Properties" section in the Quickstart guide for some examples how to set up properties.
I want to do something like following which is not allowed
myMockedObject.SetupSet(x => x.SomeMethodReturningCustomer().Age = 18);It returns
Expression is not a property setter invocation.
Just for the record, in recent versions of Moq, this will produce a more helpful exception:
System.ArgumentException: Unsupported expression:x => x.SomeMethodReturningCustomer()...
The next member after the last one shown above is non-virtual, sealed, or not visible to the proxy factory.