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.

Unintended consequences from implicitly implemented interfaces

See original GitHub issue

In 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 implements ICollection<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 via List<int> or via ICollection<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:closed
  • Created 7 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
thomaslevesquecommented, Sep 30, 2016

Thanks @adamralph

Since we all agree, let’s revert.

0reactions
adamralphcommented, Sep 30, 2016

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.

Read more comments on GitHub >

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

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