Follow @RoyOsherove on Twitter

FakeItEasy or NSubstitute? Which should I use for samples in art of unit testing 2nd edition?

In my upcoming 2nd edition of art of unit testing, I’ve decided not to use RhinoMocks (too old and stuck in the past), or Moq (although widely used, I think it represents the past, not the future) for examples of an open source free framework. I think .NET needs to move past Moq and Rhinomocks and seek out new frameworks that adopt some newer API and thinking principles, and are not tied down to backward compatibility APIs.

Two good upcoming frameworks are FakeItEasy and NSubstitute. If I’m missing a good newcomer, let me know in the comments.

Their APIs are slick and simple, and they represent, to me, the future of the isolation framework space in .NET in terms of usability, and also in terms of APIs that don’t use the words ‘mock” or “stub” at all. I like it because that makes less room for abusing the API to make confusing tests.


NSubstitute’s API:


var calculator = Substitute.For<ICalculator>();
//Set a return value:
calculator.Add(1, 2).Returns(3);
Assert.AreEqual(3, calculator.Add(1, 2));
//Check received calls:
calculator.Received().Add(1, Arg.Any<int>());
calculator.DidNotReceive().Add(2, 2);
//Raise events
calculator.PoweringUp += Raise.Event();


FakeItEasy, other than simple stuff, brings a whole new class of APIs - cross-cutting concerns, almost:

var session = A.Fake<ISession>();
       var criteria = A.Fake<ICriteria>();
       var controller = new ThingController(session);
       var result = controller.GetThings(“filter”);
       A.CallTo(() => criteria.Add(
           A<ICriterion>.That.Matches(c => c.ToString() == Restrictions.InsensitiveLike(“Property”, “%filter%”).ToString()).Argument))


But, I like them both a lot. I use them both in my courses. So help me choose which one to use for examples in my book in chapter 5.

I will still cover ALL other frameworks, but I have to give examples in one of them, and not confuse the reader with too many different fframeworks examples.

Answer this poll:

Video: (part 2a) Building a Go Game Engine with TDD and Pair Programming

HOW TO: Find the temp videos screenflow makes when uploading to vimeo and youtube