moq icon indicating copy to clipboard operation
moq copied to clipboard

SetupSet on hierarchy

Open AbhishekTripathi opened this issue 12 years ago • 4 comments

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

AbhishekTripathi avatar Aug 05 '14 21:08 AbhishekTripathi

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?

metabacalhau avatar Jun 10 '15 08:06 metabacalhau

@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.

stakx avatar Jul 02 '17 18:07 stakx

@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.

stakx avatar Jul 02 '17 18:07 stakx

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.

stakx avatar Aug 11 '19 08:08 stakx