NSubstitute 的限制是什么,特别是与最小起订量相比?

What are NSubstitute limitations, specially vs MOQ?

我即将为我的下一个项目做出关于模拟库的决定。

因为我是这些图书馆的新手,所以我进行了快速搜索

我发现 MOQ is much popular than NSubstitute 并且我希望得到社区的更多帮助,特别是在 SO

但我喜欢NSubstitute syntax more, also it has a nice docs

所以我的问题是"Is there any thing that I can achieve using MOQ I cant achieve using NSubstitute?"

我不知道 nsubstitute 有任何限制

几年前我是最小起订量的行家,现在我更喜欢nsubstitute。我喜欢语法(你直接调用方法与设置。),我认为 NSubstitute 具有最好的语法并且是所有框架中最可读的(但这是一个主观断言^^)。

哦,也许有一件事:NSubstitute 没有严格的模拟模式(但我一直认为这是一个坏主意,所以我从未将其视为限制)

我注意到,当尝试使用 .Do() 模拟对 NSubstitute 中方法的调用时,您只能将参数用作对象数组。在 Moq 中,您可以强制参数的数量、类型和名称。

例如:

  • NSubstitute: .Do(param => new ExObject{ s = (string) param[0], i = (int) param[1] })
  • 起订量:.Callback< string, int>((text, nb) => new ExObject{ s = text, i = nb })

我发现在 Moq 中更容易理解,因为您可以更轻松地阅读参数,而不必去数它是哪一个。

https://github.com/moq/moq4#moq 说:

Moq also is the first and only library so far to provide Linq to Mocks

例如,NSubstitute

中的相同行为
var mockDateTimeProvider = Substitute.For<IDateTimeProvider>();
mockDateTimeProvider.Now.Returns(new DateTime(2020, 02, 28));

可以在Moq一行内完成!

var mockDateTimeProvider = Mock.Of<IDateTimeProvider>(i => i.Now == new DateTime(2020, 02, 28));

You can think of Linq to Mocks as "from the universe of mocks, give me one whose behavior matches this expression".