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

Difference between Fact and Theory? - xUnit.net

I'm new to xUnit.net and AutoFixture. I'm currently working on a "testproject" to get familiar with xUnit.net and Autofixture. There is one little thing I don't understand. What is the difference between [Fact] and [Theory, AutoMoqData]? Could you…
sternze
  • 1,524
  • 2
  • 9
  • 15
74
votes
2 answers

Using Verify to confirm expected parameter values in Moq mock class

I'm trying to verify that a method within a mock is called with an expected object parameter. I'm using Moq, nUnit, and thinking that AutoFixture's Likeness should get the job done. Below is a simplified version of what i'm trying to do. Is there…
jaminto
  • 3,895
  • 3
  • 32
  • 36
62
votes
3 answers

Can't grasp the difference between Freeze/Inject/Register

Before starting, I'm a big fan of AutoFixture, I'm still in the curve of learning how to use the tool. So thanks for having developed Autofixture Mr Ploeh and all the contributors. So let's start with my question. According to AutoFixture/AutoMoq…
John
  • 4,351
  • 9
  • 41
  • 57
61
votes
11 answers

AutoFixture - configure fixture to limit string generation length

When using AutoFixture's Build method for some type, how can I limit the length of the strings generated to fill that object's string properties/fields?
Ricardo Rodrigues
  • 2,198
  • 2
  • 18
  • 22
52
votes
1 answer

What is AutoFixture AutoMoq?

I was looking at nuget and wanted to import moq when I noticed AutoFixture AutoMoq. I see that AutoFixture is to help write TDD faster but I can't find any examples of AutoMoq and how it is different then AutoFixture. Can someone point me to this…
chobo2
  • 83,322
  • 195
  • 530
  • 832
42
votes
2 answers

How to combine AutoDataAttribute with InlineData

I heavily use the Autofixture AutoData Theories for creating my data and mocks. However this prevents me from using the InlineData Attributes from XUnit to pipe in a bunch of different data for my tests. So I am basically looking for something like…
zlZimon
  • 2,334
  • 4
  • 21
  • 51
39
votes
2 answers

Autofixture customizations: provide constructor parameter

I have the following class: class Foo { public Foo(string str, int i, bool b, DateTime d, string str2) { ..... } } I'm creating a Foo with AutoFixture: var foo = fixture.Create(); but I want AutoFixture to provide a known…
Orlando Osorio
  • 3,116
  • 7
  • 29
  • 52
38
votes
1 answer

Autofixture: Controlling number of elements that are created of type string[]

I have an issue with creating a string array of type string[], everytime it creates 3 values but i want to be able to control this. I am using var tst = fixture.Create(); I also looked into using CreateMany but that seemed to return a…
Martin
  • 23,844
  • 55
  • 201
  • 327
38
votes
2 answers

How can I instruct AutoFixture to not bother filling out some properties?

I have a set of Data Access classes that are nested fairly deep. To construct a list of 5 of them takes AutoFixture more than 2 minutes. 2 minutes per Unit test is way to long. If I was coding them by hand, I would only code up the ones I need, so…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
31
votes
6 answers

AutoFixture.AutoMoq supply a known value for one constructor parameter

I've just started to use AutoFixture.AutoMoq in my unit tests and I'm finding it very helpful for creating objects where I don't care about the specific value. After all, anonymous object creation is what it is all about. What I'm struggling with…
Nick Soper
  • 765
  • 1
  • 5
  • 12
30
votes
3 answers

Automocking Web Api 2 controller

I am trying to auto mock ApiController class in my test cases. It worked perfectly when I was using WebApi1. I started to use WebApi2 on the new project and I am getting this exception thrown after I try to run my new…
user2910739
  • 313
  • 3
  • 7
29
votes
3 answers

Ploeh AutoFixture was unable to create an instance from System.Runtime.Serialization.ExtensionDataObject

We have an MVC project with references to WCF services. Those references added (ExtensionDataObject)ExtensionData property to every DTO and Response object and now AutoFixture fails to create anonymous instances of these types. Example: public…
Ruslan
  • 9,927
  • 15
  • 55
  • 89
28
votes
3 answers

Setting property value on child instance to a fixed value with Autofixture

Is it possible to assign a fixed value to a property on a child instance when building a parent with Autofixture? It will add default values to all the properties on the child instance like a charm, but I would like to override and assign a specific…
Jens Andresen
  • 283
  • 1
  • 3
  • 6
27
votes
7 answers

Is there a Java alternative to Ploeh's AutoFixture for .Net?

I am looking for a Java tool that would create anonymous variables (variables whose value I don't care about) in my tests, similar to AutoFixture in .Net. Here is a link to AutoFixture's readme, which has pretty good examples of what it does. Here…
Ivan Alagenchev
  • 552
  • 6
  • 13
26
votes
2 answers

How to use AutoFixture to build with customized properties while keeping type customizations?

I am trying to use autofixture to create an object but there are certain properties that I want to always be defaulted (while the rest could be auto generated). However, whenever I setup an customization it gets overwritten when I build with…
Ben Anderson
  • 7,003
  • 4
  • 40
  • 40
1
2 3
41 42