jcoliz.FakeObjects 1.1.0

dotnet add package jcoliz.FakeObjects --version 1.1.0                
NuGet\Install-Package jcoliz.FakeObjects -Version 1.1.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="jcoliz.FakeObjects" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add jcoliz.FakeObjects --version 1.1.0                
#r "nuget: jcoliz.FakeObjects, 1.1.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 jcoliz.FakeObjects as a Cake Addin
#addin nuget:?package=jcoliz.FakeObjects&version=1.1.0

// Install jcoliz.FakeObjects as a Cake Tool
#tool nuget:?package=jcoliz.FakeObjects&version=1.1.0                

Fake Objects

This is a .NET Core 3.1 library for creating fake objects used in tests. It can be used in applications built on any version of .NET from Core 3.1 onward, including .NET 6.0.

Contributor Covenant Build+Test

Usage

Namespace

using jcoliz.FakeObjects;

Mark Up Classes

First, you'll need to tell the library which properties you want filled in on your fake objects. Do this by marking them "Editable(true)". In the example below, the library will only fill in values for the Name and Date properties.

using System.ComponentModel.DataAnnotations;

public class ModelItem
{
    public int ID { get; set; }

    [Editable(true)]
    public string Name { get; set; }

    [Editable(true)]
    public DateTime Date { get; set; }

    public string Details { get; set; }
}

Create objects

In its simplest form, you can simply make a number of objects. Each item will have a unique value in the editable fields, and default values in the others.

[TestMethod]
public void AddItems()
{
    // Given: A set of objects
    var items = FakeObjects<ModelItem>.Make(20);

    // When: Adding objects to the system
    repository.AddRange(items);

    // Then: The items were added
    var results = repository.GetItems();
    Assert.IsTrue(results.SequenceEqual(items));
}

Modify certain objects

Commonly, we want a subset of objects to have a certain property we're looking for. Each call to Make or Add creates another group, starting at zero. In this example, Group(1) will contain the 10 items with "__test__" appended to the name.

[TestMethod]
public void Search()
{
    // Given: A set of objects, where a subset has {word} in the name
    var word = "__test__";
    var items = FakeObjects<ModelItem>.Make(20).Add(10, x => x.Name += word);

    // And: The objects are in the repository
    repository.AddRange(items);

    // When: Searching for {word}
    var results = repository.Search(word);
    
    // Then: Only the objects with {word} are returned
    Assert.IsTrue(results.SequenceEqual(items.Group(1)));
}

Add objects to the system under test

Any time we are adding objects to the system under test during the "Given" phase of a test, it's a good time to use the "SaveTo()" feature. In this example, we implement IFakeObjectsSaveTarget directly in the test class. You can also implement it directly, for example in a mock repository of items.

using System.Collections;

[TestClass]
public class TestClass: IFakeObjectsSaveTarget
{
    public void AddRange(IEnumerable objects)
    {
        if (objects is IEnumerable<ModelItem> items)
            repository.AddRange(items);
        else
            throw new NotImplementedException();
    }

    [TestMethod]
    public void SearchV2()
    {
        // Given: A set of objects in the repository, where a subset has {word} in the name
        var word = "__test__";
        var items = FakeObjects<ModelItem>.Make(20).Add(10, x => x.Name += word).SaveTo(this);

        // When: Searching for {word}
        var results = repository.Search(word);
        
        // Then: Only the objects with {word} are returned
        Assert.IsTrue(results.SequenceEqual(items.Group(1)));
    }
}

And more!

Please review the Usage Tests for the complete reference of use cases.

Code of conduct

We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone. We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.

Please review the Code of conduct for more details.

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 netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.

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
1.1.0 564 5/19/2022
1.0.0 935 3/15/2022
1.0.0-rc1 162 3/15/2022

* Now fills in values for  properties