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
9
votes
1 answer

AutoFixture fails to CreateAnonymous MVC Controller

The code: IFixture fixture = new Fixture().Customize(new AutoMoqCustomization()); fixture.Customize(c => c.Without(x => x.ModelMetadata)); var target = fixture.CreateAnonymous(); the…
Ruslan
  • 9,927
  • 15
  • 55
  • 89
9
votes
2 answers

How to let Autofixture create an instance of a type that contains properties with an interface type?

I have such a class: public class ViewModel { public IPagination List { get; set; } // interface! public SearchFilter SearchFilter { get; set; } public string Test { get; set; } } public class SearchFilter { public string Name…
Rookian
  • 19,841
  • 28
  • 110
  • 180
8
votes
1 answer

Use value of a parent property when creating a complex child in AutoFixture

I'm using AutoFixture to generate data for a structure involving a parent object and complex child objects, like this: public class Parent { public int Id { get; set; } public string Name { get; set; } public Child[] Children { get; set;…
8
votes
1 answer

AutoFixture Customize vs Build

I know I can use AutoFixture to create an auto-mocked instance using var person = fixture.Create(); But, if I want to customise the way a Person is created I have several options. One is to use Build var person = fixture.Build() …
Owain Williams
  • 2,387
  • 17
  • 22
8
votes
2 answers

How to combine a Convention-based Customization with AutoFixture's [AutoData] attribute?

I am using AutoFixture's [AutoData] attribute to provide some unit tests (NUnit) with an instance of a POCO. For example: [Test, AutoData] public void Create_NameIsNull_ThrowsException(MyPOCO myPOCO) {..} I've recently added a new string property…
urig
  • 16,016
  • 26
  • 115
  • 184
8
votes
1 answer

XUnit and AutoFixture Exception No Data found for (test name)

I have a very simple test as shown below. I try to freeze my two dependencies using the AutoDataAttribute + AutoMoqCustomization. class AutoMoqDataAttribute : AutoDataAttribute { public AutoMoqDataAttribute() : base(new…
user4478810
8
votes
1 answer

Create a fixture for recursive data structure with AutoFixture

I'm working on a project where I have some recursive data structure and I want to create a fixture for it. The data structure is XmlCommandElement, it has a single method ToCommand that converts XmlCommandElement to Command. Each node on the tree…
iam3yal
  • 2,188
  • 4
  • 35
  • 44
8
votes
1 answer

C# How to simplify unit testing string parameters using AutoFixture

I'm trying to create a simple way to test string parameters in unit tests, for most string parameters, I want to check the behaviour when the parameter is Null, Empty or consists only of whitespaces. In most of the cases, I'm checking the…
Dennis
  • 1,810
  • 3
  • 22
  • 42
8
votes
2 answers

Assembly version conflicts for AutoFixture and Moq with NUnit on TeamCity 7

I previously had all unit tests for my solution contained in a single library, and were recently split out. When located in a single assembly, all tests passed both locally and on TeamCity, but when seperated there are version…
Rob King
  • 1,131
  • 14
  • 20
8
votes
1 answer

What does CreateMany with seed do?

What does the CreateMany overload with the T seed parameter actually do? I've tried to seed, but the seed seems to have no effect on the created objects. For example, I was expecting that if my seed had a property of type string, that either:…
Suraj
  • 35,905
  • 47
  • 139
  • 250
8
votes
3 answers

How to combine PropertyData and AutoNSubstituteData attributes in xunit/autofixture?

I am using the [AutoNSubstituteData] attribute, which was posted here: AutoFixture, xUnit.net, and Auto Mocking I would like to combine this with the [PropertyData("")] attribute from xunit extensions. This is my test: public static…
mrt181
  • 5,080
  • 8
  • 66
  • 86
8
votes
1 answer

Freeze enum value in AutoFixture

We have an enum: enum Letters { A, B, C, D, E } When I try: var frozenLetter = fixture.Freeze(Letters.D); Strangely, frozenLetter == A. var letter = fixture.Create(); var anotherLetter =…
jcorcoran
  • 253
  • 1
  • 9
8
votes
1 answer

Customizations vs Residue collectors

I don't really grasp the difference between Customizations and Residue Collectors. According to Documentation, if I register a customization that can build, ExampleClass it will handle requests for that type that were not handled by other builders.…
joozek
  • 2,143
  • 2
  • 21
  • 32
8
votes
2 answers

Always Freeze mocks using AutoFixture, XUnit, and Moq

I'm using AutoFixture, Moq, and XUnit extensions ([Theory] attribute) as described in this blog post http://blog.ploeh.dk/2010/10/08/AutoDataTheorieswithAutoFixture. I've noticed that most of unit tests look like this: [Theory, AutoMoqData] public…
Omar
  • 39,496
  • 45
  • 145
  • 213
8
votes
1 answer

Why does Autofixture w/ AutoMoqCustomization stop complaining about lack of parameterless constructor when class is sealed?

When I use Moq directly to mock IBuilderFactory and instantiate BuilderService myself in a unit test, I can get a passing test which verifies that the Create() method of IBuilderFactory is called exactly once. However, when I use Autofixture with…
Jeff
  • 2,191
  • 4
  • 30
  • 49