Comments (8)
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.
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.
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.
@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.
@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.
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.
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.
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)
- Stop using Moq as a guinea pig to get feedback on and develop SponsorLink HOT 105
- Support Static Abstract property HOT 1
- Community Involvement HOT 5
- Change log is not updated since version 4.18.4 HOT 3
- Mocking Blobclient.DownloadContentAsync() HOT 3
- Poll on SponsorLink HOT 1
- Should allow null references as constructor parameters. HOT 1
- Replacement for Moq.MethodCallReturns'2 HOT 3
- The public API is still missing nullable reference type annotations HOT 1
- a minor changes to improve the readability HOT 2
- Mock verification slow with It.Is(Expression) HOT 5
- [Feature Request] Make it possible to moq concrete classes HOT 1
- Moq dependency uprev is not working as expected HOT 1
- SponsorLink Integration Makes Moq Unuasable due to PII and GDPR Requirements HOT 9
- Add the ability to compare ref argument by value HOT 1
- Cannot cast inside the Action<T> parameter of Verify method
- Wrong Setup by using String interpolation within It.Is<string> HOT 4
- Question: would it make sense to call verify all on a strict mock when the mock gets disposed?
- `DefaultValueProvider` extensibility
- Some source code files is filled with comments like: "Unmerged change from project "
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moq4.