35

I'm about to take a decision about the mocking library for my next project.

and because I'm new to those libraries I made a quick search

I found that MOQ is much popular than NSubstitute and I expect more help from the community specially here at SO

But I liked NSubstitute syntax more, also it has a nice docs.

So my question is "Is there any thing that I can achieve using MOQ I cant achieve using NSubstitute?"

George Botros
  • 4,127
  • 3
  • 28
  • 50
  • 5
    [This post might be helpful](http://weareadaptive.com/blog/2014/09/30/why-nsubstitute/), particularly the section titled "NSubstitute tradeoffs". – David Tchepak Jan 18 '16 at 22:01

3 Answers3

45

I am not aware of any limitation of nsubstitute

Few years ago I was an adept of moq, and now I have a preference for nsubstitute. I like the syntax (you call directly the method vs setup.), I think NSubstitute has the best syntax and is the most readable of all the frameworks (but this is a subjective assertion ^^).

Oh maybe one thing : NSubstitute don't have a strict mock mode (but I always thought it was a bad idea, so I never saw it as a limitation)

rad
  • 1,857
  • 1
  • 20
  • 30
  • 1
    There is a slight limitation of NSubstitute in comparison to Moq: It cannot stub `protected` functions. This becomes relevant, when trying to replace the `HttpMessageHandler` in `HttpClient`. It is possible to make this work, but it is a limitation. – Jerome Reinländer May 18 '20 at 07:07
  • 2
    Probably now, if Moq will not revert this change, NSubstitute will have waaaay more users: https://github.com/moq/moq/issues/1372 – 0x49D1 Aug 09 '23 at 05:46
6

I noticed that when trying to mock a call to a method in NSubstitute by using .Do(), you can use the parameters only as an array of objects. In Moq you can force the number, types and names of parameters.

For example:

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

I found it easier to understand in Moq, as you can read more easily the parameters, rather than to have to count which one is it.

Cosmin
  • 124
  • 1
  • 5
3

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

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

For example, this same behavior in NSubstitute

var mockDateTimeProvider = Substitute.For<IDateTimeProvider>();
mockDateTimeProvider.Now.Returns(new DateTime(2020, 02, 28));

Can be achived in Moq in one-line!

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".

dan
  • 1,545
  • 3
  • 20
  • 29
  • 11
    Though it is one liner it is still readable with NSubstitute than Moq – Navap Sep 13 '20 at 09:45
  • I agree with @Navap , the Moq might be a one liner, but it is not obvious what is the result of the setting, I would prefer NSubstitute here as even without looking into a doco I can understand what it does. – Vočko May 17 '23 at 02:28