FakeItEasy.AutoFakeIt 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package FakeItEasy.AutoFakeIt --version 1.0.0
NuGet\Install-Package FakeItEasy.AutoFakeIt -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FakeItEasy.AutoFakeIt" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FakeItEasy.AutoFakeIt --version 1.0.0
#r "nuget: FakeItEasy.AutoFakeIt, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FakeItEasy.AutoFakeIt as a Cake Addin
#addin nuget:?package=FakeItEasy.AutoFakeIt&version=1.0.0

// Install FakeItEasy.AutoFakeIt as a Cake Tool
#tool nuget:?package=FakeItEasy.AutoFakeIt&version=1.0.0

FakeItEasy.AutoFakeIt

Build codecov

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

Getting started

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, wether 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 
            // 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 😃

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 29,363 9/26/2022
1.0.2 1,722 6/23/2021
1.0.1 377 6/23/2021
1.0.0 348 6/22/2021