Creating a substitute

The basic syntax for creating a substitute is:

var substitute = Substitute.For<ISomeInterface>();

This is how you’ll normally create substitutes for types. Generally this type will be an interface, but you can also substitute classes in cases of emergency.

Warning: Substituting for classes can have some nasty side-effects. For starters, NSubstitute can only work with virtual members of the class, so any non-virtual code in the class will actually execute! If you try to substitute for your class that formats your hard drive in the constructor or in a non-virtual property setter then you’re asking for trouble. If possible, stick to substituting interfaces.

With the knowledge that we’re not going to be substituting for classes, here is how you create a substitute for a class that has constructor arguments:

var someClass = Substitute.For<SomeClassWithCtorArgs>(5, "hello world");

For classes that have default constructors the syntax is the same as substituting for interfaces.

Substituting for multiple interfaces

There are times when you want to substitute for multiple types. The best example of this is when you have code that works with a type, then checks whether it implements IDisposable and disposes of it if it doesn’t.

var command = Substitute.For<ICommand, IDisposable>();
var runner = new CommandRunner(command);

runner.RunCommand();

command.Received().Execute();
((IDisposable)command).Received().Dispose();

Your substitute can implement several types this way, but remember you can only implement a maximum of one class. You can specify as many interfaces as you like, but only one of these can be a class. The most flexible way of creating substitutes for multiple types is using this overload:

var substitute = Substitute.For(
		new[] { typeof(ICommand), typeof(ISomeInterface), typeof(SomeClassWithCtorArgs) },
		new object[] { 5, "hello world" }
	);
Assert.IsInstanceOf<ICommand>(substitute);
Assert.IsInstanceOf<ISomeInterface>(substitute);
Assert.IsInstanceOf<SomeClassWithCtorArgs>(substitute);

Substituting for delegates

NSubstitute can also substitute for delegate types by using Substiute.For<T>(). When substituting for delegate types you will not be able to get the substitute to implement additional interfaces or classes.

var func = Substitute.For<Func<string>>();

func().Returns("hello");
Assert.AreEqual("hello", func());

Partial substitutes and test spies

When required we can also create substitutes that run real code by default, letting us replace specific parts of a class with substitute behaviour.