---
title: "\"Swapping\" instead of \"Injecting\" calls between classes"
url: "https://osherove.com/blog/2008/5/17/quotswappingquot-instead-of-quotinjectingquot-calls-between.md"
canonical: "https://osherove.com/blog/2008/5/17/quotswappingquot-instead-of-quotinjectingquot-calls-between.html"
published: "2008-05-17T18:01:15"
updated: "2008-05-17T18:01:15"
tags: [".NET", "Agile", "Art Of Unit Testing", "Unit Testing"]
author: "Roy Osherove"
description: "Eli Lopian, Typemock CEO and awesome Coder, just created a nice little API wrapper around Typemock Isolator that would allow a very simple and readable \"Swapping\" effect between classes. It would allow you to write code…"
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/"
---

# "Swapping" instead of "Injecting" calls between classes

By Roy Osherove May 17, 2008 In [.NET](https://osherove.com/blog/category/net), [Agile](https://osherove.com/blog/category/agile), [Art Of Unit Testing](https://osherove.com/blog/category/art-of-unit-testing), [Unit Testing](https://osherove.com/blog/category/unit-testing)

**Historical article** · Originally published May 17, 2008; technical details may be outdated.

Eli Lopian, [Typemock CEO](http://www.typemock.com/) and awesome Coder, [just created a nice little API wrapper](http://www.elilopian.com/2008/05/15/ruby-style-mocking-in-net/) around [Typemock Isolator](http://www.typemock.com/) that would allow a very simple and readable "Swapping" effect between classes. It would allow you to write code like this:

    \[Test\]

       public void SwapStatic\_CallsFake()

       {

           Swap.StaticCalls<TypeA, TypeB\>();

           Assert.AreEqual(2, TypeA.StaticReturnOne());

           Swap.Rollback();

       }

       \[TestMethod\]

       public void SwapInstance\_CallsFake()

       {

           Swap.NextNew<OriginalClass, FakeClass\>();

           OriginalClass swappedInstance \= new OriginalClass();

           Assert.AreEqual(2, swappedInstance.ReturnOne());

       }

The first test uses the "Swap" class to replace static calls and redirect them to your own class with static methods. The second one is more advanced and will mock all **instance** calls made on the **next instance that will be created of that type.** so the last line on the second test will actually call a method against the FakeClass type.

pretty cool and very readable. [download the code and binaries from his post](http://www.elilopian.com/2008/05/15/ruby-style-mocking-in-net).

## More in .NET

[All posts in .NET](https://osherove.com/blog/category/net)

- May 21, 2008

    ### [Weird error message](https://osherove.com/blog/2008/5/21/weird-error-message.html)

- May 19, 2008

    ### [Two Faced Commits - how the ALT.NET Community is becoming more and more dogmatic](https://osherove.com/blog/2008/5/19/two-faced-commits-how-the-altnet-community-is-becoming-more.html)

- May 15, 2008

    ### [Typemock Isolator 4.2.4 is out](https://osherove.com/blog/2008/5/15/typemock-isolator-424-is-out.html)

- May 10, 2008

    ### [NullObject.For<T> - As simple as it gets, but no simpler](https://osherove.com/blog/2008/5/10/nullobjectforlttgt-as-simple-as-it-gets-but-no-simpler.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/
