GithubHelp home page GithubHelp logo

fakethat's Introduction

Ridiculously simple mocking for C#

Build Status

// Create a fake Death Star
var fakeStar = new Fake<IDeathStar>();

// Set up the fake so that a call to Shoot(Planet) always misses
var shootCalls = fakeStar.Stub(fakeStar.Object.Shoot, (Planet planet) => "Haha, missed!");

// give the fake Death Star to a real Vader (the class we wish to test)
var vader = new Vader(fakeStar.Object);
vader.GetAngry();

// Check whether our stubbed method was indeed called. We can use plain LINQ and
// any preferred unit testing / assertion library. No need to learn any special 
// mocking-framework assertion/verification syntax.
shootCalls.Count.ShouldBe(2);

// We have full access to the call history.
shootCalls[0].Arg1.Name.ShouldBe("Alderaan");
shootCalls[0].ReturnValue.ShouldContain("Haha,");
shootCalls.Last().Arg1.Name.ShouldBe("Naboo");

Fancy Should* assertion methods courtesy of the excellent Shouldly library. Note that you don't at all need Shouldly to use Fake That.

Description

Fake That is a simplistic mocking library for C# that allows you to stub methods on mock objects with delegates or lambda expressions. This means that unlike with other mocking frameworks, you just write snippets of code in which you check whether the stubbed method has been called with the expected arguments and return appropriate values. It's like writing a stub class, except without the boilerplate.

Though heavily inspired by Moq and FakeItEasy, Fake That is different in that it is comparably low on weird syntax. Fake That favours plain C# code and .NET data structures over "almost-like-English" fluent APIs that just can't do what you want it to do right now.

For example:

Return values and validate arguments with plain C#

Other libraries have extensive fluent syntax for specifying that a method may be called only with these and these arguments, and should return such and such a value. In Fake That, you do this in a lambda expression that contains plain old C# code.

var fakeStar = new Fake<IDeathStar>();

var shootCalls = fakeStar.Stub(fakeStar.Object.Shoot, (Planet planet) =>
{
    // Validate arguments using normal C# code
    planet.ShouldNotBe(null);

    // Make the return value depend on the arguments
    return (planet.Name.Contains("oo")) ? "BOOM!" : "Haha, missed!";
});

Note that you should specify the types of all arguments in the lambda expression (e.g. (Planet planet) => .. and not just planet => ..). This is because C# can't figure out which overload of Shoot you want to stub without those parameter types (even when there's no overloads).

Full, strongly-typed, access to call history

Every Stub call returns a CallHistory object with an IEnumerable Calls property.

var shootCalls = fakeStar.Stub(fakeStar.Object.Shoot, (Planet planet) => "Haha, missed!");

// .. call code that would call fakeStar.Object.Shoot()

shootCalls
	.Where(call => call.Arg1 == "Alderaan")
	.All(call => call.ReturnValue.Contains("Haha,")
	.ShouldBe(true);

Stub property getters and setters just the same

Getters:

var isArmedCalls = fakeStar.StubGetter(() => fakeStar.Object.IsArmed, (bool armed) => true);

vader.GetAngry();

isArmedCalls.Count.ShouldBe(1);

Setters:

var targetCalls = fakeStar.StubSetter(v => fakeStar.Object.Target = v, (Planet planet) => 
{
    planet.Name.ShouldBe("Alderaan");
});

vader.GetAngry();

targetCalls.Count.ShouldBe(1);

Installation

Getting the binaries

Currently: Compile from source. On the command line, do:

  • xbuild FakeThat.sln (Mono)
  • msbuild FakeThat.sln (.NET)

Alternatively, open the solution in your favourite IDE. FakeThat.sln is a Visual Studio 2010 solution, and I expect that it also works in MonoDevelop.

The resulting FakeThat.dll will work on both Mono and .NET.

TODO:

  • Download binaries
  • Install from NuGet

Supported platforms

  • .NET 4 and higher
  • Mono 2.8 and higher

.NET 3.5 support is currently lacking, but could be provided relatively easily with some conditional compilation. Please file an issue if you need this (or a pull request, of course).

I have no clue to what extent all the half-assed .NET frameworks (Silverlight, Windows Phone, Win RT, Client Profile) support Fake That. If you find out, please let me know. Thanks!

Background

I wrote Fake That because other C# mocking libraries made my upper back itch, and that's a very difficult place to scratch.

Some design goals include:

  • Be simple enough to require very little documentation
  • Fully typechecked: no strings for method names and all that
  • Comfortably stay outside the "Mocks vs Stubs" discussion by allowing any usage patten you prefer.

Please let me know if you think that I managed.

Credits

Fake That was written by Egbert Teeselink. It uses the excellent Castle DynamicProxy to do all the hard object-generating work.

Fake That is released under the MIT License.

Thanks to [Travis CI]((https://travis-ci.org/eteeselink/fakethat) for a free build server, and to danlimerick for a guide on how to use it for (not officially supported) .NET/Mono code.

fakethat's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

matiasnamendola

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.