Questions tagged [autofixture]

AutoFixture is an open source library for .NET designed to minimize the 'Arrange' phase of your unit tests. Its primary goal is to allow developers to focus on what is being tested rather than how to setup the test scenario, by making it easier to create object graphs containing test data.

Introduction

AutoFixture is designed to make Test-Driven Development more productive and unit tests more refactoring-safe. It does so by removing the need for hand-coding anonymous variables as part of a test's Fixture Setup phase. Among other features, it also offers a generic implementation of the Test Data Builder pattern.

Overview

When writing unit tests, you typically need to create some objects that represent the initial state of the test. Often, an API will force you to specify much more data than you really care about, so you frequently end up creating objects that has no influence on the test, simply to make the code compile.

AutoFixture can help by creating such Anonymous Variables for you. Here's an example:

[Test]
public void Echo_WithAnonymousInteger_ReturnsSameInteger()
{
    // Arrange
    Fixture fixture = new Fixture();
    int expectedNumber = fixture.Create<int>();
    MyClass sut = fixture.Create<MyClass>();

    // Act
    int result = sut.Echo(expectedNumber);

    // Assert
    Assert.AreEqual(expectedNumber, result, "The method did not return the expected number");
}

This example illustrates the basic principle of AutoFixture:

AutoFixture can create values of virtually any type without the need for you to explicitly define which values should be used.

The number expectedNumber is created by a call to Fixture.Create<T>, which will create a regular integer value, saving you the effort of explicitly coming up with one.

The example also illustrates how AutoFixture can be used as a SUT Factory that creates the actual System Under Test.

Resources

The AutoFixture project is hosted on GitHub at github.com/AutoFixture, where you can find more sample code and documentation.

625 questions
8
votes
1 answer

Autofixture Fixture.Build().With() On Same Property Name

When i set property with "with" method, its leave null all propertys on nested objects which same named. (im using autofixture's latest version as 3.0.8) public class Something { public string Id { get; set; } public IList Things…
Oğuzhan Topçu
  • 571
  • 2
  • 8
  • 17
8
votes
1 answer

Can AutoFixture be swamped by a deep and circular object graph

The domain model I am working with has a lot of circular references. In fact it is possible to get to most objects from any point in the graph. Many of these circular references are also in collections. So a Booking will have a collection of…
Keith Bloom
  • 2,391
  • 3
  • 18
  • 30
8
votes
1 answer

How can I Freeze a null instance in AutoFixture

I'm using Autofixture as a SUT factory and am having difficulty Freezing null instances. I'd like to do something like: _fixture.Freeze(c => null); but quickly realized that was wrong. I've worked around the problem using…
Peter McEvoy
  • 2,816
  • 19
  • 24
7
votes
1 answer

AutoFixture Likeness - compare only matching properties

I want to be able to compare the two following objects for likeness using AutoFixture.SemanticComparison: public class Object1 { public int a; } public class Object2 { public int a; public int b; } Now, when I do it this way: var o1 = new…
7
votes
1 answer

Does AutoFixture support `DateOnly` for .NET6?

I'm getting exception on constructing DateOnly variables/fields with AutoFixture. (constructing of TimeOnly works fine) AutoFixture.ObjectCreationExceptionWithPath : AutoFixture was unable to create an instance from System.DateOnly because creation…
Evgeny
  • 791
  • 7
  • 15
7
votes
3 answers

How to force AutoFixture to create ImmutableList

In System.Collections.Generic there is a very useful ImmutableList. But for this type Autofixture is throwing an exception because it doesn't have public constructor, it's being created like new List().ToImmutableList(). How to tell…
user963935
  • 493
  • 4
  • 20
7
votes
2 answers

How can I use AutoFixture to create an object but not fill out any properties

Using AutoFixture I can easily create an instance of a data object using the Create method like so: _fixture.Create() Using this technique I am protected for any changes in the constructor that may come in the future, but it also fills…
Robba
  • 7,684
  • 12
  • 48
  • 76
7
votes
2 answers

Can Autofixture create an anonymous type?

Suppose I'd like a call in a unit test to return an anonymous type that looks like this - var anonymousType = { id = 45, Name="MyName", Description="Whatever" } Could Autofixture generate anonymousType? If so, what's the syntax?
Rich Bryant
  • 865
  • 10
  • 27
7
votes
1 answer

AutoFixture's AutoData attribute with Xunit throws System.InvalidOperationException

I'm using xunit v2.1.0, xunit.extensions v1.8.0.1549, AutoFixture v3.40.0, AutoFixture.Xunit v3.40.0 and I have this simple trivial test that uses AutoData using Ploeh.AutoFixture.Xunit; using Xunit; namespace Tests { public class ToolTests …
dragan.stepanovic
  • 2,955
  • 8
  • 37
  • 66
7
votes
1 answer

AutoFixture setup interface property inside class

How can I have AutoFixture populate properties in my object containing interface property? public class Car : ICar { public string Name { get; set; } public ICarinformation ContactInformation { get; set; } // problematic …
ShaneKm
  • 20,823
  • 43
  • 167
  • 296
7
votes
1 answer

Create an EF entity stub with AutoFixture

For example I've got these partial classes that was generated by EF Database First: Dog: (EF entity) public partial class Dog { public int DogID { get; set; } public string Name { get; set; } public int Age { get; set; } public int…
AsValeO
  • 2,859
  • 3
  • 27
  • 64
7
votes
1 answer

AutoConfiguredMoqCustomization and unsettable properties

How do I force AutoFixture, that has been configured with AutoConfiguredMoqCustomization, to automatically mock interfaces and its read-only properties? To make things clear, let's assume I have such an interface: public interface A { int…
SOReader
  • 5,697
  • 5
  • 31
  • 53
7
votes
2 answers

Sharing AutoFixture across tests

Is sharing the instance of Fixture across multiple test methods a good practice? Or is it better to create a new instance of Fixture for every test method? What is the best practice? It will be good if you can provide me with a source on which one…
user3117252
  • 389
  • 1
  • 5
  • 11
7
votes
2 answers

Select specific constructor with AutoFixture

I'm using AutoFixture and I'd like to use a specific constructor. I have the following code and I like to select the constructor with ITemplateParameterHandler. public sealed class TemplateSegmentHandler : ITemplateSegmentHandler { public…
iam3yal
  • 2,188
  • 4
  • 35
  • 44
7
votes
2 answers

InlineAutoDataAttribute but for NUnit (for TestCase and TestCaseSource)

To be succint: class AutoMoqDataAttribute : AutoDataAttribute { public AutoMoqDataAttribute() : base(new Fixture().Customize(new AutoMoqCustomization())) { } } public interface IWeapon { bool LaunchAtEarth(double probabilityOfHitting);…
Raif Atef
  • 2,878
  • 1
  • 25
  • 31