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 the FakeItEasy.Mef assembly which lets you specify how fakes are created and configurations for fakes:
http://ondevelopment.blogspot.com/2009/11/configuring-fake-objects-on-global.html
- Created the scoping feature explained here: http://ondevelopment.blogspot.com/2009/09/fake-scopes-explained.html
- You can now create fake objects in another way: http://ondevelopment.blogspot.com/2009/09/other-way-of-faking-it.html
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:
Configure.Fake(foo).CallsTo(x => x.Bar()).Returns("test");Alternatively you can use the Configure extension method in the namespace Legend.Fakes.ExtensionSyntax:
foo.Configure().CallsTo(x => x.Bar()).Returns("test");When matching calls you can mix argument validations and concrete arguments that must match exactly:
foo.Configure().CallsTo(x => x.Bar(Argument.Is.Any<string>(), "second argument")).Throws(new Exception());
Return values can be produced at call time:
int counter = 0; foo.Configure().CallsTo(x => x.Baz()).Return(() => counter++);
Faking a class that takes arguments to constructor, no untyped object array, safe for refactoring:
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:
ThisCall.To(foo).WithAnyArguments().Throws(New Exception()) : foo.Bar(null, null)
Asserting on a "Sub" call in VB:
ThisCall.To(foo).WithAnyArguments().AssertWasCalled(Function(repeat) repeat > 0) : foo.Bar(null, null)
Configuring a "Function" in VB is just like in C#:
Configure.Fake(foo).CallsTo(Function(x) x.Baz()).Returns(10)