You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanted to update from Moq 4.16.1 to 4.18.1, but then noticed that suddenly some of our tests fail with the new version. The affected tests all show the same pattern:
create mock for an interface
call SetupAllProperties
define a value for a property using mock.Setup(x => x.property).Returns(value)
later directly set a value on the property, e.g. mock.Object.property = newvalue
In 4.16.1 this would return newvalue, since 4.17.1 it returns (old) value.
Example:
publicinterfaceIPerson{stringName{get;set;}}[TestFixture]publicclassTests{[Test]publicvoidPropertyBehaviorTest(){varpersonMock=newMock<IPerson>().SetupAllProperties();// give all properties "property behavior"
personMock.Setup(x => x.Name).Returns("Ryan");// use Setup to define property valuevarperson= personMock.Object;
Assert.That(person.Name, Is.EqualTo("Ryan"));
person.Name ="Not Ryan";// set value using property behavior
Assert.That(person.Name, Is.EqualTo("Not Ryan"));// fails; property will still return "Ryan"}}
Is this scenario supported and just broke in 4.17.1? Or should the old behavior never have worked in the first place? (I know it's probably not ideal to mix "Setup" and direct property setters).
The text was updated successfully, but these errors were encountered:
This is expected behavior. You first told your mock to stub all properties (including Name), but then told it to return "Ryan" for Name... and that's what it does from then onwards. Later setups always have preference over earlier ones; the one for Name essentially "shadows" the stubbed property getter.
Perhaps in this particular case you should simply initialize the property to "Ryan" by assigning that value to it, instead of creating a setup?
I wanted to update from Moq 4.16.1 to 4.18.1, but then noticed that suddenly some of our tests fail with the new version. The affected tests all show the same pattern:
SetupAllProperties
mock.Setup(x => x.property).Returns(value)
mock.Object.property = newvalue
In 4.16.1 this would return newvalue, since 4.17.1 it returns (old) value.
Example:
Is this scenario supported and just broke in 4.17.1? Or should the old behavior never have worked in the first place? (I know it's probably not ideal to mix "Setup" and direct property setters).
The text was updated successfully, but these errors were encountered: