Coder Social home page Coder Social logo

createandfake / createandfake Goto Github PK

View Code? Open in Web Editor NEW
11.0 4.0 1.0 765 KB

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

License: Mozilla Public License 2.0

Batchfile 0.01% C# 99.94% PowerShell 0.03% Shell 0.02%
create fake testing tests stubs mocking mocks mock data random

createandfake's Introduction

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

createandfake's People

Contributors

werebunny avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

alex-held

createandfake's Issues

Add Randomizer.CreateFor Customization

Expected Behavior

CreateFor returns randomized data for a method, and can easily be customized as well as invoked.

Current Behavior

Just returns data to pass into the method call.

Possible Solution

Wrap the returned data as a dictionary with the parameter name alongside the method.

Additional Information

To help easily test very big methods.

Also add functionality to convert to a list of args for easy verification.

Add RandomDataAttribute To Code

Expected Behavior

User can use [Theory, RandomData] without creating the RandomDataAttribute themselves.

Current Behavior

They copy the implementation from the tests or docs.

Possible Solution

Just add it, and hide the xUnit dependency.

Additional Information

The attribute is only available for xUnit, but the whole framework must work without having an xUnit dependency forced. Hiding the xUnit dependency is actually possible and still have everything compile without it. Only when a user adds the RandomData does an error pop up if the dependency is not present.

Multiple Injection Fails

Expected Behavior

Mocks are used when injecting into testers.

Current Behavior

When injecting multiple mocks into Tester behavior, the mocks aren't properly used and instead a random instance is.

Possible Solution

Steps to Reproduce

  1. Create class that takes multiple parameters that can be randomized.
  2. Run Tester method with multiple mocks.
  3. Verifies mocks never used.

Additional Information

Fluent Arg Matching for Value Types

Expected Behavior

Using value types work with fluent Arg dynamic matching.

Current Behavior

Fails to match.

Possible Solution

Equality fails due to object wrap.

Set editorguidelines

Expected Behavior

VS 2019 has a code cleanup feature that can be used to format code automatically.

Current Behavior

No set standards, individuals can apply how they wish.

Possible Solution

Create the .editorguidelines and run it on the code.

Additional Information

Add document for developers detailing how to setup code cleanup to automatically run on save.

Tester: Prevent Parameter Mutation

Expected Behavior

Tester tool can prevent parameter mutation side effects.

Current Behavior

Absent.

Possible Solution

Similar to guarding null references.

Additional Information

Unintended mutation can create nasty defects that are usually hard to find, since modification happening at one point might not be caught until much later. Tracing it back to the method with side effects can be difficult.

Add 'Also' to Fluent Assert

Wanted Behavior

Ability to test different values inside the fluent chain.

Possible Solution

valueA.Assert().Is(x).Also(valueB).Is(y);

Support Injected Classes

Expected Behavior

Classes can be automatically injected by stubs or mocks by the Faker. Given class T, this would return Injected which would contains a populated T, and a method to X get, X being a type that was injected into T.

Current Behavior

Missing.

Possible Solution

Just reflect on constructor and Fake classes.

Additional Information

Less coupling.

Randomize Child Parent References

Expected Behavior

Randomizer can create objects with a child member that references it.

Current Behavior

InfiniteLoopException thrown.

Possible Solution

Keep references with the chainer and utilize them when creating children.

Mocking Extensions

Expected Behavior

Extensions that allow mocks to be set up like:
doSomething(x, y).Returns(5);

Current Behavior

Must go through .Setup methods with Behavior.

Possible Solution

Provide Faker.Mimic.
Mimics go to a different meta provider that utilizes a static call log.
Create extensions that add special notes to the call log to change behavior.

Additional Information

Syntactic sugar, and give the people what they want.

Enable Static Tools Customization

Expected Behavior

Default Tools can be customized.

Current Behavior

User must create their own custom tools.

Possible Solution

Implement a ReaderWriterLockSlim around them, and allow hints to be added to the Tools at large through delegates.

Additional Information

This will make the library easier to use and customize, instead of having users create their own Tools file with their customized Tools.

Collection Size Attribute

Wanted Behavior

Randomizer can be provided a specific size for collection creation directly and via attribute.

Possible Solution

Add collection hint interface for hitns.

Additional Information

Update & Modernize

Update version and dependencies, look into github builds. Make sure Linux development works.

Expected Behavior

Versions are up to date and builds on github.

Current Behavior

Versions are old and uses AppVeyor/Travis.

Possible Solution

Check for most recent versions and build processes.

Randomizer Conditions

Expected Behavior

Randomizer can create objects that match conditions.

Current Behavior

User manages it.

Possible Solution

Use the Limiter with a supplied action.

Additional Information

Makes it easier to create test data to match wanted test conditions.

Fluent Assert Fake Integration

Wanted Behavior

When using Fluent assertions, can verify fakes are called before checking equality cases.

Possible Solution

Extension methods for the assertion classes that verify called behavior and return the chainer.

Additional Information

Verifying the fakes were called first verifies the code behaved as expected. If equality is checked first, the values can be off but the user won't know if the fake behavior was called properly. Verifying behavior first then value prevents this.

Fix Publish Workflow

Expected Behavior

Publish workflow automatically pushes nuget package.

Current Behavior

See error on action.

Smart Randomization

Expected Behavior

Randomize strings with common names (such as Name, City, or Cell) to look like actual data.

Current Behavior

Strings are currently just composed of random characters.

Possible Solution

Create a CreateHint that checks property names, and uses prepared lists or rules for them. Probably with an intermediate "ContactInfo" class for holding more consistent data.

Additional Information

Makes random data easier to debug and more in line with how the data might be used.

Redesign Documentation

Expected Behavior

Documentation should be use focused and have more user focused discussion and answers. For example, like adding a faq with common questions.

Current Behavior

Feature and content focused.

Possible Solution

Good example would be a faq, while using docfx (see #53: Add DocFX) to handle much of the content automatically.

Additional Information

Should be done after #52: Enable Static Tools Customization, since that'll drastically change how users would want to extend behavior and functionality, making it a good point for better documentation.

Faker Base Calls

Expected Behavior

Ability to call base implementation is possible.

Current Behavior

Absent.

Possible Solution

Will likely need to be emitted.

Additional Information

Maybe make the default behavior for stubs.

Test Host Crashing

Expected Behavior

Tests run outside VS without issues.

Current Behavior

Occasionally, the test host will crash.

Possible Solution

Likely issue on finalizer thread with Tester code.

Steps to Reproduce

  1. Run tests on command line.

Additional Information

Similar to earlier issue encountered when mocking finalizers and disposable methods.

Random Fakes

Expected Behavior

Fakes can be randomized to create fakes that return random results from all methods.

Current Behavior

Behavior is not modified for fakes when randomized.

Possible Solution

Create custom hint for Randomizer, as opposed to previous notion to place it on the Faker.

Additional Information

Part of the plan to enable test attributes to handle more mocking setup as well.

Use Seeds For Default Randoms

Wanted Behavior

Asserts display the seed used for randomization, and seeded random is used as default so test runs can be replicated.

Possible Solution

Switch to SeededRandom and drill down seed access to assert exceptions.

Additional Information

Performs just as fast and will allow easier reproduce-ability of intermittent failures.

Asserter Can Give Details For Fakes

Expected Behavior

Fakes can be provided to Asserter to give detailed debugging info.

Current Behavior

Details only provided for verify methods.

Possible Solution

Use ToString(), and enable multiple detail messages for Asserter.

Additional Information

Reduces need to cache result to verify the mocks before the returned result.

Netcore 3.0 = Bad IL Format

Expected Behavior

CreateAndFake fakes work when using .netcore 3.0.

Current Behavior

throws System.BadImageFormatException : Bad IL format.

Possible Solution

Add .netcore 3.0 as a build target.

Additional Information

Possibly .netcore 3.0 picks the .netstandard version, under which the emit library doesn't match.

Randiffer To Mutator

Expected Behavior

Randiffer is called Mutator.

Current Behavior

Randiffer is used.

Possible Solution

Change naming.

Additional Information

Mutator defines the intended and future behavior better, which is that it will also be able to mutate existing objects.

Update Nuke

Expected Behavior

Nuke uses latest version with better versioning scheme.

Current Behavior

Old version, manual version updates, and package settings in csproj file.

Additional Information

Add Task Support

Expected Behavior

Tools should support Tasks.

Current Behavior

Tasks throw errors.

Possible Solution

Create hints for task support.

Additional Information

Consider Cake Builds

Expected Behavior

Build process uses Cake.

Current Behavior

Build process uses batch scripting.

Additional Information

Cake is cross-platform.

More Fluent Assert

Wanted Behavior

Add special behavior for strings, delegates (throw), fail, and comparables.

Possible Solution

Add custom AssertObjects for them.

Additional Information

Better default error messages instead of just using the Is method.

Tester Auto Call Feature

Expected Behavior

Tester should have a special test case on just injecting up a class with stubs, then just calling every method and verifying success.

Current Behavior

Missing.

Possible Solution

Wait for Injected class support, then just reflect down methods passing in random data.

Additional Information

This tests basic layer classes that just forwards calls.

Add Top Level Auto Caller

Expected Behavior

Tester can use the auto passthrough using only declared methods on the top type.

Current Behavior

Tester will always call all methods on the type.

Possible Solution

Filter the methods.

Additional Information

For testing subclasses that have big base classes where the base behavior doesn't need to be included (or is problematic).

Add Fluent Asserts

Expected Behavior

Give option to access global tool Asserter via fluent behavior.

Current Behavior

Used through Tools.Asserter.

Possible Solution

Add global object extension .Assert.

Additional Information

Give the people what they want.

Add DocFX

Expected Behavior

Build generates documentation based upon doc comments, and the site documentation site shows them.

Current Behavior

Missing.

Possible Solution

Use DocFXTasks in Nuke. Integrating DocFX into the src project itself creates a lot of crud in the src.

Additional Information

Better documentation for free, since doc comments are done.

InfiniteLoopException - Limit Depth?

Hi,

I have a very rich (broad and deep) object model for which I'm trying to generate test data. CreateAndFake is currently showing the best results with the least effort of any comparable library, but I'm running into an "InfiniteLoopException".

Now the error message provides a good explanation as to why I'm encountering the issue and my object model does have recursive types (a->b->c->a). However, as I don't really care about generating objects after the second generation (a->b) I was wondering whether it is possible to limit the depth of generator or simply ignore the "InfiniteLoopException" such that the generator continues to the next property (of whatever object it's generating) as if generation had completed successfully.

Hope thats clear??

I've tried looking into the Limiter implementation but it doesn't seem to be designed to handle this use case.

Anyway, thanks for the library. Really hope this issue can be resolved/worked around as the CreateAndFake looks very promising.

Cheers,
Ian

Replace Nuke

Wanted Behavior

Workflows work with build, tests, and coverage.

Possible Solution

Replace Nuke with Bullseye.

Additional Information

Nuke breaks with non-descriptive errors using current directory.props and is more complicated than necessary anyways.

Faker Out/Ref Support

Expected Behavior

Parameters marked 'out' or 'ref' can be mocked.

Current Behavior

They can't.

Possible Solution

Likely needs to be bundled into CallData.

Remove Fake.Setup CallData Return

Expected Behavior

Method is void type, and CallData is internal.

Current Behavior

Method returns CallData, and CallData is public.

Additional Information

Design was more relevant before fakes could automatically verify all set up behaviors. Can potentially still save some redundancy in some edge cases, but the additional complexity and forgoing encapsulation of the CallData type is likely not worth the trade-off.

Consider xUnit Tests

Expected Behavior

Tests use xUnit.

Current Behavior

Tests use MSTest.

Additional Information

Integrating the Randomizer with theories shows potential for cleaner tests.

Randomizer Changes Statics

Expected Behavior

Randomizer only randomizes instance members.

Current Behavior

Randomizer will randomize static members.

Possible Solution

Fix scope of member reflection methods.

Steps to Reproduce

  1. Create class with mutable static member.
  2. Create using randomizer.
  3. Observe change on static member.

Consider Coverlet Coverage

Expected Behavior

Code coverage uses Coverlet.

Current Behavior

Code coverage uses OpenCover.

Additional Information

Coverlet is cross-platform while OpenCover is Windows only.

Mutator: Modify

Expected Behavior

Mutator should be able to modify the state of objects.

Current Behavior

Mutator can only create variants.

Possible Solution

Create variants of writable members and set them.

Additional Information

Hints will likely be used in the future to control this behavior, but shouldn't be used until deciding how to do #1, since it's possible hints might be used for that as well.

Randomizer Guid Support

Expected Behavior

Randomizer can generate guids.

Current Behavior

It can't.

Possible Solution

Add it to common system types to create.

Additional Information

Guids are frequently used so supporting them is important.

Randomizer: Injecting

Expected Behavior

Tool that can handle creating objects automatically with fakes.

Current Behavior

Can only randomize objects.

Additional Information

Big picture is for this tool to handle test attributes to compliment randomization.

Inheritance Check Performance

Expected Behavior

Good performance.

Current Behavior

Inheritance type check just from the test project's use runs nearly ten million times, and it's recursive check that can happen multiple times for almost every action. It is the time sink.

Possible Solution

Cache type inheritance.

Additional Information

A fix here can drastically improve the performance of the library.

New Tool: Tester

Expected Behavior

Handles automatic testing for common behavior.

Current Behavior

Absent.

Possible Solution

Like the Asserter.

Additional Information

First feature is to ensure a method either runs or throws ArgumentNullException instead of NullReferenceException when every parameter is tested with nulls/random values.

Tester Injection

Expected Behavior

The Tester tool can use injection to handle creating objects while still running the full featured tests.

Current Behavior

Tester just randomizes for creating objects, thus if injection is needed only part of the test behavior can be done by passing in an instance.

Possible Solution

Use the Randomizer injector and allow passing in objects for Tester tests.

Mutator: Uniques

Expected Behavior

Mutator can create objects that have no shared values.

Current Behavior

Absent.

Possible Solution

Break down an object by value and exclude those values from randomization.

Additional Information

Enables tests to know which data set a value comes from.

Uri & DateTimeOffset Support

Expected Behavior

Uri and DateTimeOffset objects can be randomized and duplicated.

Current Behavior

Not supported.

Possible Solution

Define behavior under common system hint.

Additional Information

Requested along with #65.

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.