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.
examples:
NSubstitute’s API:
//Create:
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>();Any.CallTo(session).WithReturnType<ICriteria>().Returns(criteria);Any.CallTo(criteria).WithReturnType<ICriteria>().Returns(criteria);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)).MustHaveHappened(Repeated.AtLeast.Once);
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: