question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 7 years ago
  • Comments:43 (42 by maintainers)

github_iconTop GitHub Comments

1reaction
thomaslevesquecommented, Jul 7, 2020

Hi @DanielRose,

You can make it a bit simpler:

A.CallToSet(() => foo.Bar).Invokes((int bar) =>
{
    A.CallTo(() => foo.Bar).Returns(bar);
    foo.BarChanged += Raise.With(foo, EventsArgs.Empty);
});

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.

0reactions
blairconradcommented, Jul 7, 2020

Smashing idea, @DanielRose. I created #1783 to track the documentation effort.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found