Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetupSet on hierarchy #124

Closed
AbhishekTripathi opened this issue Aug 5, 2014 · 4 comments
Closed

SetupSet on hierarchy #124

AbhishekTripathi opened this issue Aug 5, 2014 · 4 comments

Comments

@AbhishekTripathi
Copy link

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

@metabacalhau
Copy link

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?

@stakx
Copy link
Contributor

stakx commented Jul 2, 2017

@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
Copy link
Contributor

stakx commented Jul 2, 2017

@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 stakx closed this as completed Jul 2, 2017
@stakx
Copy link
Contributor

stakx commented Aug 11, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants