Giter Club home page Giter Club logo

Comments (8)

sloncho avatar sloncho commented on July 29, 2024 1

Also, there is already a mechanism to get another interface and set it:

var mock = new Mock<IMyCollection>(MockBehavior.Strict);
var ienum = mock.As<IEnumerable>();
ienum.Setup(e => e.GetEnumerator()).Returns(list.GetEnumerator());

from moq4.

sloncho avatar sloncho commented on July 29, 2024

Interesting, this works. What do I miss?

        public interface IMyCollection : IEnumerable<int>
        {

        }

        [Test]
        public void TEST()
        {
            //arrange
            var list = new List<int> {1, 2, 3};
            var mock = new Mock<IMyCollection>(MockBehavior.Strict);
            mock.Setup(m => m.GetEnumerator()).Returns(list.GetEnumerator());

            //act
            var first = mock.Object.First();

            //assert
            first.Should().Be(1);
        }

from moq4.

rubberduck203 avatar rubberduck203 commented on July 29, 2024

Real world example of when some syntactic sugar would be nice.

        var refs = new Mock<References>();
        refs.Setup(r => r.GetEnumerator()).Returns(ReferenceList());
        refs.As<IEnumerable>().Setup(r => r.GetEnumerator()).Returns(ReferenceList);

This is a situation where to properly set up the mock, we need to repeat ourselves and provide the same implementation twice.

from moq4.

stakx avatar stakx commented on July 29, 2024

@tkellogg, @rubberduck203: Do you still wish to follow up on this, or can we close this? I guess the original question has been answered?

@rubberduck203: When you implement IEnumerable<T> manually, you will also have to provide separate implementations for IEnumerable.GetEnumerator and IEnumerable<T>.GetEnumerator. Simply because they are different methods with differing signatures. I don't see any generally useful syntactic sugar that would allow you to set up unrelated methods in one go (apart from something really coarse-grained like mock.SetupEverything()). What kind of syntactic sugar did you have in mind?

In my opinion, the problem here does not lie with Moq missing something, but with the .NET Framework having something too much. IEnumerable has been mostly a useless PITA ever since the generic IEnumerable<T> was introduced in .NET 2.0.

from moq4.

rubberduck203 avatar rubberduck203 commented on July 29, 2024

@stakx I don't know how now works under the hood. If you believe it would be a burden to create & maintain then I would close this. It's not a terrible burden to setup both methods once you understand it. When I originally faced this issue I spent 6 hours in a state of utter confusion though. I believe this could be solved with an update to the documentation.

from moq4.

stakx avatar stakx commented on July 29, 2024

Fair enough. So basically, what you are saying is that .As<TInterface>() is not easy enough to discover if you don't know it already? I suppose in that case, an update to the documentation would seem a good idea.

I am not against an improvement, I was just under the false impression that this issue was specifically about IEnumerable and IEnumerable<T> and I didn't see a general solution.

If you have any suggestions what kind of new syntax could help this, let's discuss it. ;)

from moq4.

rubberduck203 avatar rubberduck203 commented on July 29, 2024

IEnumerable & IEnumerable were just my specific use case (although, likely the most likely one). I could see a default behavior (as a user) of setting up both the visible & hidden method on a single call, but understand if it's not reasonable to implement. I'll add something to the QuickStart. You can close this as far as I'm concerned.

from moq4.

stakx avatar stakx commented on July 29, 2024

I have some trouble understanding what exactly is being asked here. The original code example probably isn't ideal because, like @sloncho already pointed out, the shown code actually works.

@tkellogg: I'll try to rephrase your issue in more general terms. Is this what you meant?

public interface IA
{
    int GetValue();
}

public interface IB : IA
{
    new int GetValue();
}

[Fact]
public static void ShadowedMembersAreSetUpLikeShadowingMember()
{
    const int expected = 42;

    var mock = new Mock<IB>();
    mock.Setup(_ => _.GetValue()).Returns(expected);

    var b = (IB)mock.Object;
    Assert.Equal(expected, actual: b.GetValue());  // => passes

    var a = (IA)mock.Object;
    Assert.Equal(expected, actual: a.GetValue());  // => fails!
}

And, if this is the problem we're talking about, are you asking that shadowed members (like A.GetValue) should work in the same way as the members shadowing them (like B.GetValue) — unless of course when they are explicitly set up via mock.As<IA>().Setup(...)?

from moq4.

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.