Giter Site home page Giter Site logo

If one interface implements another interface and both contains the same property the property is not set on the original object that implements the first interface about fakeiteasy HOT 7 CLOSED

imshz avatar imshz commented on May 21, 2024
If one interface implements another interface and both contains the same property the property is not set on the original object that implements the first interface

from fakeiteasy.

Comments (7)

philippdolder avatar philippdolder commented on May 21, 2024

Can you please also provide your test code? (The part you are describing as step 2 to 5)

from fakeiteasy.

imshz avatar imshz commented on May 21, 2024

Hi, Okey. Here is the code:

TestShouldWork do fail. Im using NUnit.

[TestFixture]
public class FakeItTests
{
    [Test]
    public void TestShouldWork()
    {
        var someClass = new SomeClass();

        var fake = A.Fake<IMyInterface>();

        someClass.SetValues(fake);

        Assert.IsNotNull(fake.Number);
        Assert.IsTrue(fake.Number == 5);
    }

    [Test]
    public void TestWorks()
    {
        var someClass = new SomeClass();

        var fake = A.Fake<IMyInterface>();

        someClass.SetValues(fake);

        Assert.IsNotNull(((IYourInterface)fake).Number);
        Assert.IsTrue(((IYourInterface)fake).Number == 5);
    }
}

public interface IMyInterface : IYourInterface
{
    decimal? Number { get; set; }
}

public interface IYourInterface
{
    decimal? Number { get; set; }
}

public class SomeClass
{
    public void SetValues(IYourInterface yi)
    {
        yi.Number = 5;
    }
}

from fakeiteasy.

philippdolder avatar philippdolder commented on May 21, 2024

I had a look into your issue just now.
I'd like to explain why FakeItEasy behaves the way it does.

The following code (manual fake implementation) shows the behavior of FakeItEasy

public class MyFake : IMyInterface
{
    int? IYourInterface.Number { get; set; }
    int? IMyInterface.Number { get; set; }
}

As you see, it implements the interfaces explicitly. Therefore the behavior differs depending on how you "look" at the object.

So far there is no way (as far as I know) to tell FakeItEasy to not implement the interfaces explicitly when there are matching methods or properties. Probably there should be a FakeOption that allows to change the implementation to explicit or non-explicit. But that's not just 2 hours of work.

For me the question in your case is: Are these your own interfaces you have to fake? Or are these 3rd party ones, that you can't change. To me the interface declaration looks like a design smell. Maybe you can change that?

Looking forward to your feedback.

from fakeiteasy.

imshz avatar imshz commented on May 21, 2024

Thank you for your feedback.

I agree that it looks like a design smell, but it is not :)

I have tried to think of other ways to reimplement my code, but the issues is this code i autogenerated. Its several classes that represents business objects and their interfaces contains these properties.
I have one interface that is to combine all common properties, and its implemented by the other interfaces.
And it is not possible for me to remove the properties from autogenerated interfaces.

I work on a large project where we are considering to change our mocking framework from RhinoMocks to something else. This is the only thing we have discovered fakeiteasy does not support that we need.

Please let me know if you decide to implement this fix in the future, we will propobly hold on to Rhino until we find something that supports every scenario with existing tests.

(sorry for my poor english)

from fakeiteasy.

philippdolder avatar philippdolder commented on May 21, 2024

Hi,

To have this feature in FakeItEasy we would need to introduce an additional Configuration possibility to let you decide if your Fake should implement the interface(s) explicitly or implicitly. In theory that could resolve your issue. But, with my current knowledge of Castle.Core (which we use to generate the dynamic proxies) and FakeItEasy internals I don't know how I could achieve a proxy that implements an interface implicitly. So, for the moment I don't think it is foreseeable that we can support this feature in the next 2 or 3 months.

But, I like to try helping you get this working with the current capabilities of FakeItEasy.

I'm still convinced that it is a design smell, but possibly it's the fault of the generated code which you probably cannot influence much.

Let me ask a couple more question regarding the design to understand your issue completely:
IMyInterface and IYourInterface are both generated code?
What tool/component are you using that generates this code?

In case IMyInterface is your code, then just don't redeclare the properties and simply write:
public interface IMyInterface : IYourInterface { }

from fakeiteasy.

adamralph avatar adamralph commented on May 21, 2024

I agree with Philipp. This is incorrect design. If an interface inherits from another interface it should not re-declare members from the parent interface. It doesn't matter whether the code is auto generated or manually written. The code as it stands is all that counts. If this is the result of auto-generation then the auto-generation needs to be fixed.

from fakeiteasy.

adamralph avatar adamralph commented on May 21, 2024

@imshz we'll leave this issue open for a while to allow you to comment.

Unless we see a compelling case for FakeItEasy to support this pattern we'll close this issue as a wontfix.

from fakeiteasy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.