admin管理员组文章数量:1125556
I have a get-only indexer defined on an interface
public interface ISomeInterface
{
string this[string key] { get; }
}
In a Unit Test with Moq it is valid for any string parameter as the indexer key:
Mock<ISomeInterface> mock = new();
mock
.Setup(m => m[It.IsAny<string>()]) // <-- valid
.Returns("anything for all string key");
Unfortunately in NSubstitute the similar seems not possible:
ISomeInterface mock = Subtitute.For<ISomeInterface>();
mock[Arg.Any<string>()] // <-- invalid
.Returns("anything for all string key");
I know that it is possible to setup the mock indexer for a specific key like in this thread:
How to mock object's indexer with private setter in NSubstitute?
How could I setup the mock indexer Return value for Any key input parameter in NSubtitute?
I have a get-only indexer defined on an interface
public interface ISomeInterface
{
string this[string key] { get; }
}
In a Unit Test with Moq it is valid for any string parameter as the indexer key:
Mock<ISomeInterface> mock = new();
mock
.Setup(m => m[It.IsAny<string>()]) // <-- valid
.Returns("anything for all string key");
Unfortunately in NSubstitute the similar seems not possible:
ISomeInterface mock = Subtitute.For<ISomeInterface>();
mock[Arg.Any<string>()] // <-- invalid
.Returns("anything for all string key");
I know that it is possible to setup the mock indexer for a specific key like in this thread:
How to mock object's indexer with private setter in NSubstitute?
How could I setup the mock indexer Return value for Any key input parameter in NSubtitute?
Share Improve this question edited 2 days ago SomeBody 8,7332 gold badges18 silver badges36 bronze badges asked 2 days ago TomcatTomcat 485 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 2Using mock[Arg.Any<string>()]
works as expected:
[Test]
public void IndexerSubTest()
{
ISomeInterface mock = Substitute.For<ISomeInterface>();
mock[Arg.Any<string>()]
.Returns("anything for all string key");
Assert.That(mock["qq"], Is.EqualTo("anything for all string key"));
}
Demo @dotnetfiddle.net
本文标签:
版权声明:本文标题:c# - How to mock indexer return value with Any string key in NSubstitute? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736662838a1946502.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
NSubstitute
and .NET are you using? It works just fine for me. – Guru Stron Commented 2 days agoSubtitute
->Substitute
– Guru Stron Commented 2 days ago