---
title: "Faking static methods in Moq, FakeItEasy and NSubstitute might be closer than imagined"
url: "https://osherove.com/blog/2012/7/8/faking-static-methods-in-moq-fakeiteasy-and-nsubstitute-migh.md"
canonical: "https://osherove.com/blog/2012/7/8/faking-static-methods-in-moq-fakeiteasy-and-nsubstitute-migh.html"
published: "2012-07-08T18:35:24"
updated: "2012-07-08T18:35:24"
tags: ["Unit Testing"]
author: "Roy Osherove"
description: "Update : the version that works with .net 4.0 is here (link to InjecttionExample3.zip in that page). the examples from the link below work in .net 3.5 and under. While I was doing my TDD course at Soluto this week, One…"
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/"
---

# Faking static methods in Moq, FakeItEasy and NSubstitute might be closer than imagined

By Roy Osherove July 8, 2012 In [Unit Testing](https://osherove.com/blog/category/unit-testing)

**Historical article** · Originally published July 8, 2012; technical details may be outdated.

> **Update**: the version that [works with .net 4.0 is here](http://www.sysnet.pe.kr/2/0/942) (link to InjecttionExample3.zip in that page). the examples from the link below work in .net 3.5 and under.

While I was doing my TDD course at [Soluto](http://soluto.com/) this week, One of the architects, Shai, Found an interesting article on CodeProject [that shows how to change the pointer of a managed method in C# to redirect to a different method](http://www.codeproject.com/Articles/37549/CLR-Injection-Runtime-Method-Replacer).

They even tried it and showed it can work to allow changing the behavior of a static method by intercepting it. This does not use the profiler API at all (like typemock or Justmock do), but it is a non-safe (unsafe) way to do it in a managed way.

This needs more examination, but if this works out, it could be the beginning of a new era of unconstrained Isolation frameworks in .NET, that are fully open source. It’s not just static methods that can be replaced, and that’s where it gets REALLY interesting.

Let’s keep our fingers crossed, and I expect the devs of Moq, FakeItEasy and NSubstitue to work on that. If they don’t , I certainly will!

## Lessons on this topic

- [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
- [Instance Delegator Focus](https://courses.osherove.com/courses/legacy-code-hero/instance-delegator-focus/) · Legacy Code Hero
- [Throw Errors with Powermock](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/throw-errors-with-powermock/) · The Art of Unit Testing and TDD in Java
- [Bogus and Class Methods](https://courses.osherove.com/courses/tdd-and-bdd-in-ruby/bogus-and-class-methods/) · TDD and BDD in Ruby
- [Isolation (Mocking) Frameworks](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-in-java/isolation-mocking-frameworks/) · The Art of Unit Testing and TDD in Java

## More in Unit Testing

[All posts in Unit Testing](https://osherove.com/blog/category/unit-testing)

- Jul 12, 2012

    ### [Video: (part 8 - The big Refactoring) TDD Pairing a Go Game Engine](https://osherove.com/blog/2012/7/13/video-part-8-the-big-refactoring-tdd-pairing-a-go-game-engin.html)

- Jul 9, 2012

    ### [Video: (part 7 - The bug that wouldn't die) Go Game Engine with TDD and Pair Programming](https://osherove.com/blog/2012/7/9/video-part-7-the-bug-that-wouldnt-die-go-game-engine-with-td.html)

- Jul 5, 2012

    ### [Video: (part 6 - Total n00b mistakes) Go Game Engine with TDD and Pair Programming](https://osherove.com/blog/2012/7/5/video-part-6-total-n00b-mistakes-go-game-engine-with-tdd-and.html)

- Jul 2, 2012

    ### [Video: (part 5 - A Test DSL) Go Game Engine with TDD and Pair Programming](https://osherove.com/blog/2012/7/2/video-part-5-a-test-dsl-go-game-engine-with-tdd-and-pair-pro.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/
