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

Force AutoFixture to use the greediest constructor

I have a data type with multiple constructors and I need AutoFixture to choose the greediest (one with the most parameters). The default behaviour is to choose the constructor with the smallest number. The author's blog post,…
RichK
  • 11,318
  • 6
  • 35
  • 49
23
votes
2 answers

AutoFixture: how to CreateAnonymous from a System.Type

I need to create an object from AutoFixture using nothing more than a System.Type. However, there doesn't appear to be an overload of CreateAnonymous() that simply takes a type. They all expect a compile time generic T. Is there a way to convert…
Mitch A
  • 2,050
  • 1
  • 21
  • 41
22
votes
3 answers

Autofixture and read only properties

Let's consider two version (one with read only properties) of the same very simple entity: public class Client { public Guid Id { get; set; } public string Name { get; set; } } vs public class Client { public Client(Guid id, string…
Marcin Konrad Ceglarek
  • 1,442
  • 2
  • 13
  • 20
19
votes
3 answers

How to implement date restrictions with AutoFixture?

I'm currently having a model class which contains several properties. A simplified model could look like this: public class SomeClass { public DateTime ValidFrom { get; set; } public DateTime ExpirationDate { get; set; } } Now I'm…
oopbase
  • 11,157
  • 12
  • 40
  • 59
19
votes
1 answer

Create anonymous enum value from a subset of all values

Let's say we have an enum type defined as: enum Statuses { Completed, Pending, NotStarted, Started } I'd like to make Autofixture create a value for me other than e.g. Pending. So (assuming round-robin generation) I'd like to…
dzendras
  • 4,721
  • 1
  • 25
  • 20
18
votes
1 answer

What are the differences between MOQ and AutoFixture?

I have a fair amount of experience using MOQ, while I've recently have stumbled into AutoFixture. What are the differences between these frameworks?
Carlo V. Dango
  • 13,322
  • 16
  • 71
  • 114
18
votes
1 answer

Customizing AutoFixture builder with seeded property

I've got a customized autofixture builder for an integration test. Code is below. Question 1 - At present the first transaction has a TransactionViewKey.TransactionId of 1, etc. How do I set the TransactionViewKey TransactionId so it is seeded…
Justin
  • 227
  • 2
  • 8
18
votes
1 answer

Easy way to specify the value of a single constructor parameter?

Is there some easy way in Autofixture to new-up an object using it's constructor, but hard-code/specify the value to use for a single parameter in the constructor? I see that this can be done with properties, using something…
Suraj
  • 35,905
  • 47
  • 139
  • 250
18
votes
2 answers

Example of how to use AutoFixture with NSubstitute

I use NSubstitute a lot. And I love it. I am just looking into AutoFixture. It seems great! I have seen AutoFixture for NSubstitute and seen a few examples in Moq on how to use this feature. But I can't seem to translate it into NSubstitute. I…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
18
votes
1 answer

AutoFixture and interfaces

Let's say I have interface: public interface IFoo { int Bar1 { get; set; } int Bar2 { get; set; } } If IFoo was class, I could write: fixture.CreateAnonymous(); and the result will have numbers set for Bar1 and Bar2. But how to do…
Pol
  • 5,064
  • 4
  • 32
  • 51
17
votes
2 answers

Use AutoData and MemberData attributes in XUnit test

I'm facing an interesting problem. I found out that the AutoDataAttribute can be use to minimize the "Arrange" part of your test (dependencies passed through the ctor). Awesome! Example: public class AutoMoqDataAttribute : AutoDataAttribute { …
user4478810
17
votes
1 answer

How to get AutoFixture create an integer that is >0, and not another number?

I want AutoFixture to generate two integers, and for the second one, I don't want it to be 0, or the previous generated number. Is there a way to tell AutoFixture to honor that "requirement". Looking at RandomNumericSequenceGenerator, I looks like…
Ties
  • 1,205
  • 2
  • 15
  • 28
16
votes
1 answer

Generate dictionary with AutoFixture

For a list, we can do fixture.CreateMany>(1000); // with 1000 elements but how to do it with a dictionary? And to be able to specify the number of elements to be generated.
John
  • 4,351
  • 9
  • 41
  • 57
16
votes
2 answers

Let AutoFixture create DateTime in UTC?

By default AutoFixture creates DateTime structs in "local, unspecified time". I have been trying to find a way to configure it to create DateTime structs with UTC kind, but so far unsuccessful. Is there a way to do this?
Jesper Lund Stocholm
  • 1,973
  • 2
  • 27
  • 49
16
votes
1 answer

Creating an AutoFixture specimen builder for a type

I'm creating an AutoFixture specimen builder for a particular type, in this case System.Data.DataSet. The builder will return a FakeDataSet, which is a customized DataSet for testing. The following doesn't work, with dataSet always returning null,…
Matt Slavicek
  • 841
  • 10
  • 18
1
2
3
41 42