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
10
votes
2 answers

Is is possible to have ITestOutputHelper created by xUnit to be available in AutoFixture context?

Is is possible to have ITestOutputHelper created by xUnit to be available in AutoFixture context? In my integration tests I use Builder class that contains helper methods for some routine operations. In order to hide complexity of class creation I…
Michael Baranov
  • 789
  • 1
  • 7
  • 17
10
votes
1 answer

How to build nested property with autofixture

How to set nested property with autofixture (it's readonly)? Something like this: var result = fixture.Build() .With(x => x.First.Second.Third, "value") .Create();
user963935
  • 493
  • 4
  • 20
10
votes
1 answer

AutoFixture refactoring

I started to use AutoFixture http://autofixture.codeplex.com/ as my unit tests was bloated with a lot of data setup. I was spending more time on seting up the data than to write my unit test. Here's an example of how my initial unit test looks like…
Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73
10
votes
2 answers

Cannot find Autofixture circular reference

I'm using Autofixture to generate my test models but in one instance it says that my model has a circular reference which I cannot find. The failing model is the class RepresentationExpense and the only thing that I can think of is it being…
Morten Christiansen
  • 19,002
  • 22
  • 69
  • 94
10
votes
3 answers

How can I add common postprocessing applied after customization

I have defined ISpecimenBuilder for my models and use it like that: new Fixture().Customize(new ModelCustomization()); I want to use it in most of my tests concerning model. I also want to apply some form of post-processing in one of my test…
joozek
  • 2,143
  • 2
  • 21
  • 32
10
votes
2 answers

Creating a struct with autofixture throws no public constructor error

I have a struct e.g.: public struct Foo { public int Bar; public string Baz; } and would like to create it in my unit tests using autofixture. I tried using the following: IFixture fixture = new Fixture(); var f =…
Rok
  • 705
  • 8
  • 20
10
votes
2 answers

AutoFixture: Configuring an Open Generics Specimen Builder

I have an object model that uses Open Generics (Yes, yes, now I have two problems; that's why I'm here :) :- public interface IOGF { } class C { } class D { readonly IOGF _ogf; public D( IOGF ogf ) { _ogf = ogf; …
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
9
votes
4 answers

AutoFixture Register type globally

I am using AutoFixture in this instance to materialize objects containing a Mongo ObjectId, like so Fixture fixture = new Fixture(); fixture.Register(ObjectId.GenerateNewId); But I am doing this for every test. Is it possible to register this…
Mark Walsh
  • 3,241
  • 1
  • 24
  • 46
9
votes
3 answers

XUnit, AutoFixture and Moq best practice

I'm reading a lot of documentation and examples about how to properly unit test things combining the three components in the title. I came up with a test method for a method on my business logic, but it feels very clunky and dirty. I'd like to get…
Matteo Mosca
  • 7,380
  • 4
  • 44
  • 80
9
votes
1 answer

How to tell Autofixture to create objects with different ID?

i have a simple Setup method in my tests thats creates two instances of an object (make by and Id and a Description properties) and i have done it with autofixture: MyObject o1 = fixture.Create(); MyObject o2 =…
gt.guybrush
  • 1,320
  • 3
  • 19
  • 48
9
votes
3 answers

Specifying [readonly] property values [via ctor args] when instantiating [immutable] objects with AutoFixture

My test requires that I set the Response property on an immutable Rsvp object (see below) to a specific value. public class Rsvp { public string Response { get; private set; } public Rsvp(string response) { Response = response; …
Matt Slavicek
  • 841
  • 10
  • 18
9
votes
1 answer

unit testing order of items in a list

How can I setup a deterministic test to verify that items in a list are ordered? First I did the following: public void SyncListContainsSortedItems( [Frozen] SyncItem[] expected, SyncItemList sut) { Assert.Equal(expected.OrderBy(x =>…
cocogorilla
  • 1,815
  • 14
  • 36
9
votes
1 answer

How to create a customization that omits the auto properties for a whole range of types?

I am trying to create a customization that allows me to specify that the properties of types that are not in a certain namespace should not be populated. Basically, I am trying to change this: fixture.Customize(c =>…
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
9
votes
5 answers

How to configure AutoFixture to use an enum value as seed when creating many of a certain type?

I have the following types: public enum Status { Online, Offline } public class User { private readonly Status _status; public User(Status status) { _status = status; } public Status Status {get {return _status; }} public…
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
9
votes
3 answers

Defining/managing >1 constructor parameters of the same type with independent values when using AutoFixture as a SutFactory

Using AutoFixture as a SutFactory, I can see that if I register or freeze a value for a type, that value will then be used for all subsequent usages of that type. However, if I have a class with two parameters that are the same type on the…
gmn
  • 4,199
  • 4
  • 24
  • 46