Git Product home page Git Product logo

fakeiteasy.autofakeit's Introduction

FakeItEasy.AutoFakeIt

Build codecov Nuget

A very simple, yet flexible, "AutoFaker" for FakeItEasy to easily auto generate classes with faked dependencies.

Getting started

NuGet package available:

PM> Install-Package FakeItEasy.AutoFakeIt

I focused on providing a familiar and very simple-to-use API, without any unnecessary limitations, so you can automatically generate your System Under Test (SUT) with all the dependencies faked, whether they are classes or interfaces. You can also customize and retrieve any dependency you want.

using FakeItEasy;
using FakeItEasy.AutoFakeIt;
using NUnit.Framework;

namespace UnitTests
{
    public interface IMyDependency
    {
        string MyMethod();
    }

    public class MyDependency : IMyDependency
    {
        public string MyMethod() => "Hello";
    }

    public class MyOtherDependency
    {
        public virtual string MyMethod() => "World";
    }

    public class MySut
    {
        public IMyDependency MyDependency { get; }
        public MyOtherDependency MyOtherDependency { get; }

        public MySut(IMyDependency myDependency, MyOtherDependency myOtherDependency)
        {
            MyDependency = myDependency;
            MyOtherDependency = myOtherDependency;
        }
    }

    public class SampleTest
    {
        [Test]
        public void MyTest()
        {
            // the main entrypoint, you can put this on a base class, as long
            // as you always create a new one for each test to make sure that
            // your tests are idempotent and isolated
            var autoFakeIt = new AutoFakeIt();

            // you can optionally provide a dependency for any type
            autoFakeIt.Provide<IMyDependency>(new MyDependency());

            // it will respect any objects you've provided before
            // you can also just let the Generate provide FakeItEasy's fakes
            // for all missing dependencies
            var mySut = autoFakeIt.Generate<MySut>();

            // you can get any dependency used/generated
            var theAutoFakedDependency = autoFakeIt.Resolve<MyOtherDependency>();

            // you can setup or assert the generated fakes since it is
            // just an ordinary FakeItEasy's fake object
            A.CallTo(() => theAutoFakedDependency.MyMethod()).Returns("Faked");
        }
    }
}

That's all you probably need from an autofaker. If you want, you can look at this project's tests to see all the possibilites.

Other options

There are many other alternatives to this package, why did I make yet another autofaker for FakeItEasy? While all the other alternatives worked, many of them had a few shortcomings related to how I expected it to work. I grew accustomed to the automocker from Moq that had a very similar API to this package.
That doesn't mean that this is the best implementation available, it just means that this is the implementation that was the most familiar to me. Hopefully some of you will find it familiar too.

My goal with this package was to provide all the features I wanted, that were missing in all the other alternatives I've tested:

  • Have a way of providing a concrete class instead of relying on a faked dependency
  • Automatically provide a fake for a concrete class (with virtual methods) instead of requiring an interface
  • Allow changing a dependency after instantiating the AutoFakeIt object
  • Allow passing dependencies or requesting generated dependencies any time

All the other packages missed some of the above.

Available alternatives

Contributing

If you have a scenario that isn't supported, please open an issue so we can discuss it :)

fakeiteasy.autofakeit's People

Contributors

akamud avatar lucasteles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fakeiteasy.autofakeit's Issues

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.