My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Links

Important

There's a breaking change in version 1.7., read more here: http://ondevelopment.blogspot.com/2011/04/old-pain-point-removed-from-fakeiteasy.html

It's faking amazing!

// Creating a fake object is just dead easy!
// No mocks, no stubs, everything's a fake!
var lollipop = A.Fake<ICandy>();
var shop = A.Fake<ICandyShop>();

// To set up a call to return a value is also simple:
A.CallTo(() => shop.GetTopSellingCandy()).Returns(lollipop);

// Use your fake as you would an actual instance of the faked type.
var developer = new SweetTooth();
developer.BuyTastiestCandy(shop);

// Asserting uses the exact same syntax as when configuring calls,
// no need to teach yourself another syntax.
A.CallTo(() => shop.BuyCandy(lollipop)).MustHaveHappened();

In this example the lollipop instance is used as a stub and the shop instance is used as a mock but there's no need to know the difference, just fake it! Easy!

Description

A .Net dynamic fake framework for creating all types of fake objects, mocks, stubs etc.

  • Easier semantics, all fake objects are just that - fakes - the use of the fakes determines whether they're mocks or stubs.
  • Context aware fluent interface guides the developer.
  • Full VB.Net support.

Designed for ease of use and for compatibility with both C# and VB.Net.

News

Added support for Silverlight, FakeItEasy-SL.dll is now included in the archive in the downloads section.

There's a breaking change in the latest version, read more on why here: http://ondevelopment.blogspot.com/2011/02/fakeiteasy-breaking-change-for.html

From now on all new versions will be built against the .Net framework 4.0. Unless someone screams really loud there will be no 3.5 builds. Version 1.0.0. will still be built against the 3.5 framework.

Updated to version 2.5 of Castle Dynamic Proxy and Castle.Core in the 1.5.3916.151 and later.

The overloads of the "Returns"-function has been renamed in the post 1.0. versions, there are methods named "Returns" and "ReturnsLazily" instead.

The resolving of dummy arguments has been made a lot stronger in the latest versions (1.5.3916.151 and later).

Syntax

Creating a fake object: You can create fake objects in two ways in FakeItEasy, either by calls to A.Fake-methods.

IFoo foo = A.Fake<IFoo>();

Or you can create a fake object, that is a wrapper around the faked object, this object provides an api for configuring and asserting on the faked object, like this:

Fake<IFoo> fake = new Fake<IFoo>();
IFoo = fake.FakedObject;

Configuring a method on the fake object to return something:

A.CallTo(() => foo.Bar()).Returns("test");

Configuring calls to any method on an object

Any.CallTo(foo).Throws(new Exception());
Any.CallTo(foo).WithReturnType<string>().Returns("hello world");

When matching calls you can mix argument constraints and concrete arguments that are matched by equality:

A.CallTo(() => foo.Bar(A<string>.Ignored, "second argument")).Throws(new Exception());

Return values can be produced at call time:

int counter = 0;
A.CallTo(() => foo.Baz()).Returns(() => counter++);

Asserting

A.CallTo(() => foo.Bar()).MustHaveHappened();
A.CallTo(() => foo.Bar()).MustNotHaveHappened();
A.CallTo(() => foo.Bar()).MustHaveHappened(Repeated.AtLeast.Once);
A.CallTo(() => foo.Bar()).MustHaveHappened(Repeated.Never);
A.CallTo(() => foo.Bar()).MustHaveHappened(Repeated.NoMoreThan.Times(4));
A.CallTo(() => foo.Bar()).MustHaveHappened(Repeated.Exactly.Twice);

Faking a class that takes arguments to constructor, no untyped object array, safe for refactoring: In order to pass arguments to the constructor of fakes of classes you'd use a lambda expression rather than the common method of passing object arrays representing the arguments. The expression will actually never be invoked so the constructor call in the following example will not be invoked but the arguments will be extracted from it.

var foo = A.Fake<Foo>(() => new Foo("string passed to constructor"));

To raise an event on a fake object:

foo.SomethingHappened += Raise.With(EventArgs.Empty).Now;

To raise an event on a fake object in VB:

AddHandler foo.SomethingHappened, AddressOf Raise.With(EventArgs.Empty).Now

'If the event is an EventHandler(Of T) you can use the shorter syntax:'

AddHandler foo.SomethingHappened, Raise.With(EventArgs.Empty).Go

Configuring a "Sub" call in VB:

NextCall.To(foo).WithAnyArguments().Throws(New Exception())
foo.Bar(null, null)

Asserting on a "Sub" call in VB:

NextCall.To(foo).WithAnyArguments().MustHaveHappened()
foo.Bar(null, null)

In .Net 4 VB supports lambda-subs as well:

A.CallTo(Sub() foo.Bar(A<object>.Ignored, A<object>.Ignored)).MustHaveHappened()

Configuring a "Function" in VB is just like in C#:

A.CallTo(Function() foo.Baz()).Returns(10)
Powered by Google Project Hosting