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
7
votes
4 answers

How to use AutoData in unit tests to supply N objects of a type in test arguments?

I use AutoData in my xUnit unit tests. I occasionally have a need for a particular number of objects to be supplied to my tests. Consider the following class: public class Recipient { public void Receive( CallingBird bird1, …
Jeff
  • 2,191
  • 4
  • 30
  • 49
7
votes
2 answers

Does AutoFixture have a mechanism to check that methods that return any kind of list never return null?

I want to write a test that checks classes within a given namespace. All methods of that classes that return any kind of list have to be checked, if they would return null. If so the test has to fail. The classes/methods itself have also…
Rookian
  • 19,841
  • 28
  • 110
  • 180
7
votes
1 answer

How do I assign a property on only a subrange of items in a list?

I want to create a list of custom objects using AutoFixture. I want the first N objects to have a property set to one value, and the remainder to have it set to another value (or to simply be set by the Fixture's default strategy). I am aware that I…
Peter
  • 3,619
  • 3
  • 31
  • 37
7
votes
3 answers

AutoFixture CompositeDataAttribute does not work with PropertyDataAttribute

I'm trying to create AutoPropertyDataAttribute based on CompositeDataAttribute from this example AutoFixture: PropertyData and heterogeneous parameters. It works with single set of parameters, but fails with more sets of parameters. Here is…
Andrej Slivko
  • 1,236
  • 2
  • 12
  • 27
7
votes
1 answer

Preventing Autofixture from filling child collections

I'm using the latest version of Autofixture, and I'd like to prevent it from filling automatically child collections. For example, I have a class Person that has a List property. I want all properties filled, except the list. I tried using this…
Pacane
  • 20,273
  • 18
  • 60
  • 97
7
votes
2 answers

Autofixture + NSubstitute: Freezing a mock?

I am trying to get access to a mocked (via Nsubstitute) class that was injected onto the constructor. I was using the following code var fixture = new Fixture() .Customize(new AutoNSubstituteCustomization()); var sut =…
Martin
  • 23,844
  • 55
  • 201
  • 327
7
votes
2 answers

What are the principles behind AutoFixture's declarative way of setting up a fixture?

I have previously asked a similar question on SO to which I got an answer. At the time, for the sake of expediency, I mechanically applied the answer but now I'm trying to get a handle on how the mechanism for declaratively setting up a fixture…
DavidS
  • 2,179
  • 4
  • 26
  • 44
7
votes
1 answer

Invoke Dispose method on AutoFixture customization

I'm using an AutoFixture customization to test a repository which access to a SQL Compact DB. Would be very helpful to me to delete this database as soon as the test is completed. Because the db is created in the customization constructor I think…
Antonio
  • 126
  • 9
7
votes
1 answer

Why doesn't simple test pass using AutoFixture Freeze, SemanticComparison Likeness and CreateProxy?

I'm trying to understand how to use the CreateProxy() feature of Likeness() using two instances of a simple class. public class Band { public string Strings { get; set; } public string Brass { get; set; } } With the following test, I use…
Jeff
  • 2,191
  • 4
  • 30
  • 49
7
votes
3 answers

AutoData Theories with AutoFixture using manual fakes

Given this system to test: public class MySut { private readonly IHardToMockDependency _hardToMockDependency; public MySut(IHardToMockDependency hardToMockDependency, IOtherDependency otherDependency) { …
TrueWill
  • 25,132
  • 10
  • 101
  • 150
7
votes
1 answer

AutoFixture.AutoMoq: set value to read only property

I use AutoFixture with AutoMoq. I try to create a fake instance of a class which has a property with a getter but without a setter. I expect AutoFixture to configure the mock so it will return the given value even there is no setter. My code is…
HerrLoesch
  • 1,023
  • 1
  • 13
  • 24
7
votes
2 answers

AutoFixture: mock methods don't return a frozen instance

I'm trying to write this simple test: var fixture = new Fixture().Customize(new AutoMoqCustomization()); var postProcessingAction = fixture.Freeze>(); var postProcessor =…
axk
  • 5,316
  • 12
  • 58
  • 96
7
votes
1 answer

Why is AutoFixture Customization causing inherited properties to not be filled?

I wrote the following customization and have it applied as part of a composite on most of my tests. My entities have a read-only Id, but I'm using their SetId method in this customization to make sure all entities have some Id if they are transient…
ventaur
  • 1,821
  • 11
  • 24
7
votes
1 answer

How do I apply AutoFixture customizations to anything inheriting from a base class?

In an attempt to DRY up my unit tests, I'm trying to use AutoFixture as an IoC container to instantiate my system under test (SUT), which in this particular case are ASP.NET MVC Controllers. Therefore, I want to customize AutoFixture to create…
Brant Bobby
  • 14,956
  • 14
  • 78
  • 115
7
votes
2 answers

How to simplify Unit Testing DDD Value objects Equality with AutoFixture

I'm currently writing Unit Tests for my Domain project with xUnit, Moq and AutoFixture. Let's have a look at this Test Method: [Theory] public void SameValueAs_OnOtherHasDifferentEmail_ReturnsFalse() { Fixture fix = new Fixture(); var sut =…
hoetz
  • 2,368
  • 4
  • 26
  • 58