Creating Fakes
Natural fakes
The common way to create a fake object is by using the A.Fake
syntax, for example:
IFoo
in this case).
You can create a fake delegate with the same syntax:
You can also create a collection of fakes by writing:
For cases where the type to fake isn't statically known, non-generic methods are also available. These are usually only required when writing extensions for FakeItEasy, so they live in the FakeItEasy.Sdk
namespace:
using FakeItEasy.Sdk;
...
var type = GetTypeOfFake();
object fake = Create.Fake(type);
IList<object> fakes = Create.CollectionOfFake(type, 10);
Explicit Creation Options
When creating fakes you can, through a fluent interface, specify options for how the fake should be created, depending on the type of fake being made:
Option | Applies to |
---|---|
Specify arguments for the constructor of the faked type | non-delegates |
Specify additional interfaces that the fake should implemen | non-delegates |
Assign additional custom attributes to the faked type | non-delegates |
Cause a fake to have strict mocking semantics | any fake |
Configure all of a fake's methods to use their original implementation | classes |
Create a fake that wraps another object | any fake |
Examples:
// Specifying arguments for constructor using expression. This is refactoring friendly!
// The constructor seen here is never actually invoked. It is an expression and it's purpose
// is purely to communicate the constructor arguments which will be extracted from it
var foo = A.Fake<FooClass>(x => x.WithArgumentsForConstructor(() => new FooClass("foo", "bar")));
// Specifying arguments for constructor using IEnumerable<object>.
var foo = A.Fake<FooClass>(x => x.WithArgumentsForConstructor(new object[] { "foo", "bar" }));
// Specifying additional interfaces to be implemented. Among other uses,
// this can help when a fake skips members because they have been
// explicitly implemented on the class being faked.
var foo = A.Fake<FooClass>(x => x.Implements(typeof(IFoo)));
// or
var foo = A.Fake<FooClass>(x => x.Implements<IFoo>());
// Assigning custom attributes to the faked type.
// foo's type should have "FooAttribute"
var foo = A.Fake<IFoo>(x => x.WithAttributes(() => new FooAttribute()));
// Create wrapper - unconfigured calls will be forwarded to wrapped
var wrapped = new FooClass("foo", "bar");
var foo = A.Fake<IFoo>(x => x.Wrapping(wrapped));
Implicit Creation Options
Implicit creation options are available, equivalent in power to the explicit creation options mentioned above.
Unnatural fakes
For those accustomed to Moq there is an
alternative way of creating fakes through the new Fake<T>
syntax. The fake provides a fluent interface for configuring the faked
object:
var fake = new Fake<IFoo>();
fake.CallsTo(x => x.Bar("some argument")).Returns("some return value");
var foo = fake.FakeObject;
For an alternative look at migrating from Moq to FakeItEasy, see Daniel Marbach's blog post that talks about Migration from Moq to FakeItEasy with Resharper Search Patterns.