---
title: "FakeItEasy or NSubstitute? Which should I use for samples in art of unit testing 2nd edition?"
url: "https://osherove.com/blog/2012/6/26/fakeiteasy-or-nsubstitute-which-should-i-use-for-samples-in.md"
canonical: "https://osherove.com/blog/2012/6/26/fakeiteasy-or-nsubstitute-which-should-i-use-for-samples-in.html"
published: "2012-06-26T00:13:40"
updated: "2012-06-26T00:13:40"
tags: ["Cool Tools", "Unit Testing", "tdd"]
author: "Roy Osherove"
description: "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…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

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

By Roy Osherove June 26, 2012 In [Cool Tools](https://osherove.com/blog/category/cool-tools), [Unit Testing](https://osherove.com/blog/category/unit-testing), [tdd](https://osherove.com/blog/category/tdd)

**Historical article** · Originally published June 26, 2012; technical details may be outdated.

In my [upcoming 2nd edition of art of unit testing](http://affiliate.manning.com/idevaffiliate.php?id=462_304), 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](http://julianjelfs.wordpress.com/2011/03/30/why-fakeiteasy-beats-rhino-mocks/) and [NSubstitute](http://nsubstitute.github.com/). 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:

## Lessons on this topic

- [Advanced Mocks and Stubs](https://courses.osherove.com/courses/tdd-in-net-in-hebrew/advanced-mocks-and-stubs/) · TDD in .NET in Hebrew
- [Isolation Frameworks](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/isolation-frameworks/) · The Art of Unit Testing & TDD Master Class in .NET
- [Isolation Frameworks](https://courses.osherove.com/courses/tdd-in-net-in-hebrew/isolation-frameworks/) · TDD in .NET in Hebrew
- [Maintainability Practices](https://courses.osherove.com/courses/tdd-in-net-in-hebrew/maintainability-practices/) · TDD in .NET in Hebrew
- [Recursive Fakes](https://courses.osherove.com/courses/tdd-in-net-in-hebrew/recursive-fakes/) · TDD in .NET in Hebrew

## More in Cool Tools

[All posts in Cool Tools](https://osherove.com/blog/category/cool-tools)

- Mar 16, 2013

    ### [Tools And Frameworks for Unit Testing in .NET for 2013](https://osherove.com/blog/2013/3/16/tools-and-frameworks-for-unit-testing-in-net-and-java.html)

- Feb 22, 2013

    ### [Six Tools for managing multiple remote desktop sessions on windows and mac](https://osherove.com/blog/2013/2/22/tools-for-managing-multiple-remote-desktop-sessions-on-windo.html)

- Jun 18, 2012

    ### [\[Cool Tool\] Chocolatey for windows is what you will not live without in two years](https://osherove.com/blog/2012/6/18/cool-tool-chocolatey-for-windows-is-what-you-will-not-live-w.html)

- Dec 14, 2011

    ### [\[Cool Tool\] Checking Out VsVim](https://osherove.com/blog/2011/12/14/cool-tool-checking-out-vsvim.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
