Unit Testing in C#
  • Unit testing in C#
  • Unit testing
    • What to test
    • When to test
    • Qualities of a good unit test suite
    • Qualities of a good unit test
    • Dealing with dependencies
    • Running the tests
  • NUnit
    • Quick glance at NUnit
    • Creating a NUnit test project
    • Anatomy of a test fixture
    • Lifecycle of a test fixture
    • Assertions
    • Asynchronous executions
    • Parameterized tests
    • Assumptions
    • Describing your tests
  • Moq
    • Quick glance at Moq
    • Method arguments
    • Method calls
    • Properties
    • Results
    • Callbacks
    • Exceptions
    • Events
    • Verifications
    • Base class
    • Mock customization
    • Implicit mocks
    • Mock repository
    • Custom matchers
    • Multiple interfaces
    • Protected members
    • Generic methods
    • Delegates
  • AutoFixture
    • Quick glance at AutoFixture
    • Fixture
    • Create and Build
    • Type customization
    • Data annotations
    • Default configurations
    • Building custom types
    • Relays
    • Tricks
    • Idioms
    • Integration with NUnit
    • Integration with Moq
    • Combining AutoFixture with NUnit and Moq
    • Extending AutoFixture
  • Advanced topics
    • Testing HttpClient
Powered by GitBook
On this page
  1. AutoFixture

Data annotations

Data annotations were introduced with ASP.NET MVC as a declarative way to describe validation rules.

By using data annotation attributes, developers can decorate their models with validation rules that ASP.NET would use to generate validation logic both on the client and on the server.

public class Contact
{
    [StringLength(10)]
    public string FirstName { get; set; }

    [StringLength(10)]
    public string LastName { get; set; }

    [Range(18, 30)]
    public int Age { get; set; }

    [RegularExpression(@"^[2-9]\d{2}-\d{3}-\d{4}$")]
    public string Telephone { get; set; }
}

AutoFixture has a built-in support for some of the data annotation attributes:

  • StringLength sets the maximum length of a string. AutoFixture will generate a string whose length is less or equal than the specified value.

  • Range sets the boundaries of a valid range of values. AutoFixture will generate a value between the specified boundaries.

  • RegularExpression specifies that a string must match a certain pattern. AutoFixture will generate a string that matches the specified pattern.

Since the support is built-in, types decorated with the supported data annotation attributes are automatically handled.

var fixture = new Fixture();
var contact = fixture.Create<Contact>();

The snippet above will generate an object whose properties will match the specified limits.

PreviousType customizationNextDefault configurations

Last updated 4 years ago