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

How can I tell AutoFixture to always create TDerived when it instantiates a TBase?

I have a deeply-nested object model, where some classes might look a bit like this: class TBase { ... } class TDerived : TBase { ... } class Container { ICollection instances; ... } class TopLevel { Container container1; …
Gary McGill
  • 26,400
  • 25
  • 118
  • 202
15
votes
1 answer

AutoFixture and private properties

Can I instruct AutoFixture to fill also private properties, annotated with a specific attribute such as Ninject.Inject, of all classes? The source seems to scan for public properties only: 1. This question provides a solution for specific MyClass…
Jani
  • 1,088
  • 1
  • 10
  • 18
15
votes
2 answers

How can I create and populate my mock classes with Autofixture?

Currently I'm using EF6 to implement my repositories inside a UnitOfWork. I also have created an In-Memory mock implementations (MockUnitOfWork & MockRepository) so that I can use them in unit tests, however I now have to deal with the tedious…
WhiskerBiscuit
  • 4,795
  • 8
  • 62
  • 100
15
votes
3 answers

Creating a domain model without circular references in Entity Framework

I have found a solution that works (using DTOs and AutoMapper), which is reproduced below, but I would prefer an answer that lists the different approaches to the problem with examples and this will be marked as the answer if received. In my entity…
jag
  • 387
  • 2
  • 14
15
votes
1 answer

Creating recursive tree with AutoFixture

I have just started using AutoFixture and have this semi-complex data structure that I would like to create some specimen for. In the tests I am working with I don't care too much about content of the data structure. I just want reasonable default…
Holstebroe
  • 4,993
  • 4
  • 29
  • 45
15
votes
3 answers

Ignore virtual properties

We have MVC4 project with Entity Framework for storage. For our tests we recently started using Autofixture and it is really awesome. Our models graph is very deep and usually creating one object by AutoFixture creates the whole graph: Person ->…
trailmax
  • 34,305
  • 22
  • 140
  • 234
14
votes
4 answers

Can Autofixture.Create return a negative value?

Actually, the following method always returns a positive integer: var fixture = new Fixture(); var someInt = fixture.Create(); Is it possible that one day, the feature evolves and begins to return a negative integer? Or is there an official…
Deblaton Jean-Philippe
  • 11,188
  • 3
  • 49
  • 66
14
votes
5 answers

Applying [AutoFixture] SemanticComparison OfLikeness to sequences / collections / arrays / IEnumerable

We have written a test which looks like the following. This test requires that we have created en Equal-overload for the CodeTableItem-class: ICollection expectedValutaList = new List(); expectedValutaList.Add(new…
13
votes
2 answers

Autofixture and Moq v4

I installed Autofixture and Moq using Nuget.So I have moq version 4. When running the following code var fixture = new Fixture().Customize(new AutoMoqCustomization()); fixture.CreateAnonymous(); the following error shows…
user256034
  • 4,289
  • 5
  • 37
  • 44
13
votes
2 answers

Bogus, AutoFixture, others(?): How to fill a nested model with fake data and set rules for specific properties?

I have a very nested model that I want to create thousands of with fake data. But, also, some properties in the model need to be in a specific range or have specific rules. I looked at these two fake data generators: AutoFixture only seems to…
Thypari
  • 801
  • 1
  • 6
  • 22
13
votes
3 answers

AutoFixture, create a list of email addresses

I'm writing some unit tests and have a class called Account which has public Guid AccountId {get;set;} public IEnumerable EmailAddresses {get;set;} etc... I want to use autofixture to create the account, but I'm having trouble getting the…
Bryan
  • 5,065
  • 10
  • 51
  • 68
13
votes
2 answers

Are integer numbers generated with AutoFixture 3 unique?

Are integer numbers generated with IFixture.Create() unique? The Wiki says that numbers are random, but it also tells us this The first numbers are generated within the range of [1, 255], as this is a set of values that are valid for all…
Pawel Krakowiak
  • 9,940
  • 3
  • 37
  • 55
13
votes
1 answer

Applying DRY to Autofixture "Build" statements

Assume I have this concrete class: public partial class User { public int ID { get; set; } public string Email { get; set; } public string FullName { get; set; } } And I want to create an anonymous instance that has a valid email…
WhiskerBiscuit
  • 4,795
  • 8
  • 62
  • 100
13
votes
3 answers

How do I use Autofixture (v3) with ICustomization, ISpecimenBuilder to deal with constructor parameter?

I'm trying to overcome a scenario in which a class has a string constructor parameter which cannot be satisfied by any old string generated by Autofixture (the Guid-y looking value). Before you're tempted to answer simply with a link to Mark…
Jeff
  • 2,191
  • 4
  • 30
  • 49
12
votes
1 answer

AutoFixture IEnumerable behavior with CreateMany()

When looking at the post here, it looks like I should be able to create several objects using CreateMany(), iterate over them using foreach, and then return them as an array. What I'm seeing is that each iteration seems to create new objects each…
rbellamy
  • 5,683
  • 6
  • 38
  • 48
1 2
3
41 42