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

AutoFixture for Open Generic Type as e.g. DbSet

I'm looking to test my code with AutoFixture. I feel that the tool has potential but it is getting hard to effectively setup just to run my first test. I'm using EF6 code first. Then, the most logical is that AutoFixture.AutoEF will do all the work…
Adanay Martín
  • 397
  • 1
  • 3
  • 15
0
votes
1 answer

AutoFixture to test MVC

Can anybody realize what I am missing here? I just want to create a controller to test. TController is a type argument of my TestFixture class. This code returns a NotImplementedException. Why? var fixture = new Fixture().Customize(new…
0
votes
0 answers

MemberAutoDataAttribute only auto generates values every second execution

I tried to combine the member-data and auto data attribute this way: using Xunit; using Ploeh.AutoFixture.Xunit2; class MemberAutoDataAttribute : CompositeDataAttribute { public MemberAutoDataAttribute(string memberName) : base( …
user6216224
0
votes
0 answers

Unit tests with new class as parametr

I have problem with mocking some method: Interface method: bool IsUserAuthorizedToAction(AuthorizationContextData contextData, AuthorizedActionType actionType); How is called: _userInformation.IsUserAuthorizedToAction(new…
Nerf
  • 938
  • 1
  • 13
  • 30
0
votes
1 answer

ReactiveUI not responding to changes in test (with Autofixture)

Look at the following codes (simple ones) public class NewEventViewModel : ReactiveObject { readonly ObservableAsPropertyHelper isSendVisible; public bool IsSendVisible { get { return isSendVisible.Value; } } …
user963935
  • 493
  • 4
  • 20
0
votes
0 answers

Is it possible to generate C# Dictionary with autofixture and injection

Is it possible to implement something like this with autofixture injection? // Fixture setup var fields = _fixture.CreateMany(1).ToList(); _fixture.Inject(fields); var kvp =…
AuthorProxy
  • 7,946
  • 3
  • 27
  • 40
0
votes
0 answers

Invoking a different constructor with autofixture

I've got a class that I am testing: public class ClassUnderTest { public ClassUnderTest() { //do stuff } public ClassUnderTest(IComplexType complexType) { //do other stuff } } When I create my…
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
0
votes
0 answers

Error AutoFixture CreateMany Method

I am writing a Unit Test using Microsot Nunit and Autofixture. I am getting a problem that When I generate a objects through AutoFixture and try to iterate over them, It does not produce result and hold on to that line. I tried to Debug it but same…
adl
  • 11
  • 1
  • 6
0
votes
0 answers

how to initialize complex object for unit testing?

How does one initialize a complex object with random data for testing? I've got kind of a complex object that looks like this: public class DynamicEntity { public string EntityName { get; set; } public List MappingEntityList { get;…
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
0
votes
2 answers

Mocking a DbConnection with NUnit/NSubstitute/AutoFixture and InsightDatabase

We're using Nunit, NSubstitute and AutoFixture to test a repository class that's built on top of Insight database... [TestFixture] public class CalculationResultsRepositoryTests { private IFixture _fixture; private IDbConnection…
James Law
  • 6,067
  • 4
  • 36
  • 49
0
votes
1 answer

Sequence contains no elements being thrown only from AutoFixture

I use AutoFixture with my BDD tests. I'm trying to write a fixture for a User class which in turn uses CentralConfiguration class. CentralConfiguration constructor looks like this: public CentralConfiguration( IConfigurationRepository…
Eedoh
  • 5,818
  • 9
  • 38
  • 62
0
votes
3 answers

Testing task delay

Not sure how to achieve this. I'm trying to unit test a method which waits a few minutes, see here: internal class JctRestartViaSmsAttemptTestRunner : ITestRunner { private readonly IMayWantMonitoring _queues; …
Rober
  • 726
  • 8
  • 27
0
votes
1 answer

Unit tests with FluentValidation. How to mock ValidationResult

I'm using TDD approach with xUnit 2, NSubstitute, AutoFixture, FluentAssertions for my unit tests. I want test my service method which using FluentValidation. Simple example: Validator: RuleSet("Nulls", () => { …
Nerf
  • 938
  • 1
  • 13
  • 30
0
votes
0 answers

Circular reference object creation throws NullReferenceException

I'm trying to create a fake of a DbSet that represents the fact table in a star schema database. Here's a simplified example where AutoFixture throws a NullReferenceException public class FctAssumption { public virtual DimAssumption…
reggaeguitar
  • 1,795
  • 1
  • 27
  • 48
0
votes
0 answers

Setting descendant property to the parent ID with AutoFixture

My Customer object has a CustomerNote property. When AutoFixture creates my Customer, it also creates a CustomerNote. My CustomerNote has a property (Guid) of the CustomerId. Without expliciting setting a variable (var cId = Guid.New //and then…
ManxJason
  • 928
  • 12
  • 33