Default behavior of read/write properties should not override explicit getter or setter configuration
See original GitHub issue(Spawned from a discussion on the chat)
I recently noticed a surprising behavior for faked read/write properties. As documented, setting the value of an unconfigured property configures it to return the assigned value. But when you do that, it also overrides any configuration made on the setter. The same is true when you read an unconfigured property.
Neither of these code snippets throw an exception:
var foo = A.Fake<IFoo>();
A.CallToSet(() => foo.Bar).To(() => A<int>.That.Not.IsEqualTo(42)).Throws(new Exception("wrong answer"));
foo.Bar = 42; // previously configured behavior is overridden here
foo.Bar.Dump();
foo.Bar = 123; // should throw here, but doesn't
foo.Bar.Dump();
var foo = A.Fake<IFoo>();
A.CallToSet(() => foo.Bar).To(() => A<int>.That.Not.IsEqualTo(42)).Throws(new Exception("wrong answer"));
foo.Bar.Dump(); // previously configured behavior is overridden here
foo.Bar = 123; // should throw here, but doesn't
foo.Bar.Dump();
We should probably consider changing this behavior, as it is counter-intuitive.
Issue Analytics
- State:
- Created 7 years ago
- Comments:43 (42 by maintainers)
Top Results From Across the Web
c# - Why is it impossible to override a getter-only property ...
It's basically just: Create a new property that has both a get and a set using the same name.
Read more >Python's property(): Add Managed Attributes to Your Classes
In this step-by-step tutorial, you'll learn how to create managed attributes, also known as properties, using Python's property() in your custom classes.
Read more >Getters and Setters: Manage Attributes in Python
You can't just override the getter method and expect the rest of the property's functionality to remain the same as in the parent...
Read more >Using Properties - C# Programming Guide
A property that has both accessors is read-write. In C# 9 and later, you can use an init accessor instead of a set...
Read more >Records - C# reference
A positional record struct declares read-write properties. You can override either of those defaults, as shown in the previous section.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @DanielRose,
You can make it a bit simpler:
It’s true that the documentation could be more explicit. The fact that the “natural” property behavior no longer applies when the property is explicitly configured is mentioned here, but maybe we could elaborate on the implications.
Smashing idea, @DanielRose. I created #1783 to track the documentation effort.