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
12
votes
3 answers

AutoFixture for number ranges

Is there an easy way to do this with AutoFixture? var myInt = fixture.Create(min, max); I would like to know whether or not this is possible with AutoFixture or if I have to instantiate a random object and do the work myself. In case this is…
Adanay Martín
  • 397
  • 1
  • 3
  • 15
12
votes
3 answers

How to fix a range on some properties when create a TestClass by AutoFixture

How can I tell AutoFixture to specify a range (min and max) on some properties when doing MyDataClass obj = fixture.Create(); where MyDataClass has property Diameter and I only want min:1 and max:60 on this property?
sowen
  • 1,090
  • 9
  • 28
12
votes
2 answers

Omitting a specific field with CreateMany from AutoFixture

I want to create "many" instances of foo : var fixture = new Fixture(); var expectedFoos = fixture.CreateMany(); The problem is, Foo is an Entity Framework entity, with relations that I don't want to create. If I needed only one instance, I…
KeatsPeeks
  • 19,126
  • 5
  • 52
  • 83
12
votes
3 answers

Controlling the depth of generation of an object tree with Autofixture

I'm trying to control the depth of generation of an object tree with Autofixture. In some cases I want just to generate the root object and in another set of cases I may want to generate the tree up to a certain depth (2, 3, let's say). class Foo { …
Jay
  • 121
  • 1
  • 4
12
votes
2 answers

AutoFixture/AutoMoq ignores injected instance/frozen mock

The short takeaway now that the solution has been found: AutoFixture returns frozen the mock just fine; my sut that was also generated by AutoFixture just had a public property with a local default that was important for the test and that…
TeaDrivenDev
  • 6,591
  • 33
  • 50
12
votes
1 answer

AutoFixture as an Automocking container vs Automocking differences?

I started to use moq but from my understanding I always have to mock up all the methods that could be called even if I really do not care about them. Sometimes it takes so long to mockup stuff you forget what you want to do. So I been looking at…
chobo2
  • 83,322
  • 195
  • 530
  • 832
11
votes
2 answers

How can I make AutoMoqCustomization use Strict MockBehavior?

Using AutoFixture with the AutoFixture.AutoMoq package, I sometimes find tests that weren't configured to correctly test the thing they meant to test, but the problem was never discovered because of the default (Loose) Mock behavior: public…
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
11
votes
1 answer

Can I re-generate random values in AutoFixture using a seed?

Is there any way in AutoFixture so that fixture.Create() will yield the same result? I.e., can I initialize the fixture with a seed? Update To be more precise, I'm looking for a random value generator that is initialised with some random…
Matthias
  • 15,919
  • 5
  • 39
  • 84
11
votes
1 answer

need to create convention for ApiControllers

I have a set of working imperative code in test and I'm trying to boil it down to an essential test convention. My test looks like the following: [Theory, BasicConventions] public void GetVersionOnSiteVersionControllerReturnsASiteVersion(IFixture…
cocogorilla
  • 1,815
  • 14
  • 36
11
votes
3 answers

Autofixture - create a float, double or decimal with remainder

How do I modify AutoFixture create method for float, double and decimal, so that when these types get created they will also have a remainder? Currently I do this, but this throws exception. var fixture = new Fixture(); fixture.Customize(sb…
Rok
  • 705
  • 8
  • 20
11
votes
2 answers

Does this test make proper use of AutoFixture and Moq?

Does this test make proper use of AutoFixture and Moq? Is it written as concisely as possible? The test fails, as expected, and passes after writing the correct implementation. [Fact] public void CustomerPropertyIsCorrect() { var fixture = new…
cocogorilla
  • 1,815
  • 14
  • 36
11
votes
1 answer

How can I emit a .NET type with two properties that are overloaded only on return type?

I need to create a type that has two properties with the same name, and only differ on return type. Dynamically emitting this type via reflection is perfectly acceptable. Something like this: public TypeA Prop { get; } public TypeB Prop { get; } I…
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
11
votes
2 answers

Autofixture test for invalid constructor parameter

I have the following class and test. I want to test passing a null value as a parameter to the constructor and are expecting an ArgumentNullException. But since I use the Autofixture's CreateAnonymous method I get a TargetInvocationException…
arwdab
  • 133
  • 1
  • 5
11
votes
3 answers

Repetitive code in unit-tests

We find ourselves coding repetitive fixture/mock setups in many test-cases - like this case: var fixture = new Fixture().Customize(new AutoMoqCustomization()); var encodingMock = fixture.Freeze>(); var httpClientMock =…
svrist
  • 7,042
  • 7
  • 44
  • 67
10
votes
1 answer

The purpose of creating anonymous types in AutoFixture for class under tests?

I recently started using AutoFixture library (http://autofixture.codeplex.com/) for Unit Testing and I quite like it. I got this code sample from the AutoFixture CodePlex website. My question is in regards to line number 8. 1. [TestMethod] 2. …
Spock
  • 7,009
  • 1
  • 41
  • 60