A C# class library that handles mocking, test data generation, and validation.


Keywords
agile, clone, clones, cloning, create, data, fake, mock, mocking, mocks, random, stubs, tdd, testing, tests, equality, randomization
License
MPL-2.0
Install
Install-Package CreateAndFake -Version 1.10.2

Documentation

Create & Fake

NuGet Build CodeCov Coverage Contributor Covenant

A C# class library that handles mocking, test data generation, and validation. Designed to handle the bulk of test setup quickly and easily so that developers can focus on the behavior to test, making tests easier to develop and maintain:

// xUnit attributes used to automatically create the parameters.
[Theory, RandomData]
internal static void TestGetMovieDirectors(
    [Fake] IStorage db, Endpoint api, [Size(2)] Details[] movies)
{
    // Setup fake behavior using randomized movie data (times optional).
    db.Find(movies[0].Name).SetupReturn(movies, Times.Once);

    // Call code and test for expected result (api is auto-injected with db fake).
    api.GetDirectors(movies[0].Name, movies[0].Year).Assert().Is(movies[0].Directors);

    // Optionally check all behavior called as expected.
    db.VerifyAllCalls();
}

By default, all behavior works not by reference but by value even for complex objects. Using the extension methods and attributes are optional as all functionality are present in customizable tools:

[Fact]
internal static void TestChangingZipCodeUpdatesAddress()
{
    // Randomizer uses readable values for some properties like 'FirstName'.
    User details = Tools.Randomizer.Create<User>();

    // Duplicator creates deep clones. 
    User copy = Tools.Duplicator.Copy(details);

    // Mutator can randomly modify objects too.
    details.ZipCode = Tools.Mutator.Variant(details.ZipCode);

    // Asserter performs checks based upon value.
    Tools.Asserter.IsNot(copy.MailingAddress, details.MailingAddress);
}

A key benefit of the library is in how the tools are logically integrated with each other. For example, the Randomizer will use stubs for interfaces that have no known implementations in the code. Or the mocks created by the Faker utilize value equality in matching arguments.

  • Asserter - Handles common test scenarios.
  • Faker - Creates mocks and stubs.
  • Randomizer - Creates random instances of any type.
  • Duplicator - Creates deep clones of objects.
  • Mutator - Mutates objects or creates variants.
  • Valuer - Compares objects by value.
  • Tester - Automates common tests.

Visit the documentation site for more information and how to get started.

Installation

  • Install using:
dotnet add package CreateAndFake
  • Use in a class by adding:
using CreateAndFake;
using CreateAndFake.Fluent;

Documentation

The documentation site is located here: https://createandfake.github.io/CreateAndFake/

The raw files can be viewed from the doc folder or built into a local site using Jekyll.

Contributing

If you're looking to contribute, thanks for your interest. Feel free to submit reports for any issues you can find, or request potential features you'd like to see here. If you wish to contribute code to the project, refer to the contributing guidelines here which includes dev environment setup. Please follow the code of conduct when contributing.

License

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

View the license file for more details.

Acknowledgments