Unintended consequences from implicitly implemented interfaces
See original GitHub issueIn 2.3.0 we resolved #477, which means that class fakes now implement the interfaces of the base class. It was supposed to be a non-breaking enhancement, but it turned out to be a breaking change:
FakeItEasy 2.2.0:
List<int> list = A.Fake<List<int>>();
list.Add(1);
list.Count.Dump(); // 1
((ICollection<int>)list).Count.Dump(); // 1
FakeItEasy 2.3.0:
List<int> list = A.Fake<List<int>>();
list.Add(1);
list.Count.Dump(); // 1
((ICollection<int>)list).Count.Dump(); // 0, oops!
Basically, what happened is this:
List<int>
implicitly implementsICollection<int>
; we used to inherit that implementation- the changes made for #477 mean that we now create the fake as if
o => o.Implements<ICollection<int>>()
was passed in the creation options - by doing this, we introduce a new, separate implementation of
ICollection<int>
, which is not configured and returns default values
So, it’s clearly a breaking change; whether it’s a good or a bad change is debatable (Moq does the same as FIE 2.3.0, NSub and RhinoMocks do the same as FIE 2.2.0), but anyway, it wasn’t our intention when we addressed #477.
We used to be able to chose between 2 behaviors:
- one where the interface members were implemented by the base class.
list.Count
was always the same, whether it was called viaList<int>
or viaICollection<int>
. It was less flexible (Count
couldn’t be configured), but it was simple to understand. It was the default behavior. - one where the members of interfaces of the faked class are now independent of the base class implementation. It’s more flexible, but also more complex and difficult to understand. This behavior was enabled by doing
o => o.Implements<ICollection<int>>()
, but is now the default behavior (for all interfaces of the faked class)
Now we can only have the second behavior.
So, should we revert, or keep the new behavior?
(in case it’s not obvious, I’m in favor of reverting)
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
c# - Is it safe for structs to implement interfaces?
Are there unintended consequences of doing so? ... Also this implicit equality is not exposed as an implementation of IEquatable<T> and thus ...
Read more >How does Go improve productivity with "implicit" interfaces, ...
The consequences of implicit interfaces are a few things. Accidental interface implementation, this can be a happy accident, ...
Read more >Hidden dangers of implicit interface implementations
Hidden dangers of implicit interface implementations ... The reason is that the class actually haven't overridden the method, because the method ...
Read more >have fake classes implement all interfaces, including ...
Workaroundable by explicitly implementing the interface by providing ... Unintended consequences from implicitly implemented interfaces #876.
Read more >Hyrum's Law
This effect serves to constrain changes to the implementation, which must now conform to both the explicitly documented interface, as well as the...
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
Thanks @adamralph
Since we all agree, let’s revert.
Neither path is particularly attractive but I think the safer option is to revert, release 2.3.1, with the removal of #477 in the release notes, re-open #477 with no milestone and re-assess it.