-
-
Notifications
You must be signed in to change notification settings - Fork 798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature - stubbing indexers #1178
Comments
I think this capability – that is, a Consider auto-implemented properties in C#: object Property { get; set; } = "initial value";
// ^^^^^^^^^^^^^ C# will automatically generate an anonymous backing field for that property. mock.SetupProperty(m => m.Property, "initial value"); When it comes to indexers, however, C# does not support the auto-implemented variety: object this[int index, ...] { get; set; }
// ^^^^^^^^^^^^^
(What would that do, anyway? Create a backing One of the principles behind Moq is that it lets you automate what you would otherwise have to spell out manually. In the same way that If we were to introduce In the few occasions where you would actually need something like a dictionary-like indexer, it's already not too hard to achieve that using the existing methods: var mock = new Mock<IFoo>();
var dict = new Dictionary<int, object>();
mock.Setup(m => m[It.IsAny<int>()]).Returns((int index) => dict[index]);
mock.SetupSet(m => m[It.IsAny<int>()] = It.IsAny<object>()).Callback((int index, object value) => dict[index] = value);
public interface IFoo
{
object this[int index] { get; set; }
} For the reasons laid out above, my personal vote goes against |
Agreed but this pull request does it for n keys. Regardless no problem. |
Like SetupProperty
The text was updated successfully, but these errors were encountered: