Pluralsight Notes – Better .NET Unit Tests with AutoFixture: Get Started

The Better .NET Unit Tests with AutoFixture: Get Started course by Jason Roberts, takes you through the need for and use of AutoFixture. AutoFixture is an open source library that helps you to create anonymous test data to accelerate your unit testing time frame. The ability that AutoFixture provides to quickly and easily generate anonymous test data is also a great addition to moving towards TDD. I am definitely a proponent of utilizing a TDD methodology, a tool like AutoFixture certainly helps to reduce the resistance by making the data setup much easier. This course will get you up and running with AutoFixture quickly, by taking you through all of the basic and some important advanced usages you may need to understand to get AutoFixture integrated into your project.

Introducing AutoFixture

  • Improve readability
    • Generate anonymous test data – data we don’t really care about
  • Improve productivity – reduce the need to manually create up test data not imperative to test itself
  • Reduce test maintenance
  • Reduce TDD friction
    • Small constant changes should not cause tests to break
  • Reduce “Arrange” test code
    • Arrange signifies the phase where the test data/state of system under test is being setup
  • Anonymous test data
    • “Data that is required to be present for the test to be able to execute, but where the value itself is unimportant.”
  • Can be used with any test framework
    • Additional nuget packages that add support for specific frameworks
  • The “Fixture” class
    • Create() – creates anonymous test data
    • Properties on custom objects will be populated with anonymous data as well

Creating Anonymous Test Data

  • Strings
    • var name = fixture.Create()
    • Generates a random “guid” for a string – sometimes this will make it difficult to understand a failed test
    • var name = fixture.Create("first_") – put in a “seed” string – prefix the anonymous value with seed value
    • var anonymousChar = fixture.Create()
  • ┬áNumbers
    • var num = fixture.Create() – suppports all numeric types (double/decimal/long/short…etc)
  • Dates and Times
    • var dt = fixture.Create()
    • var ts = fixture.Create()
  • Sequences and Complex Types (Collections) – Also applies to Custom Objects
    • var msgs = fixture.CreateMany()
    • var msgs = fixture.CreateMany(10) – create 10 strings (3 is default)
    • fixture.AddManyTo(sut.Messages, 10) – generate 10 strings and add to existing collection on system under test
    • fixture.AddManyTo(sut.Messages, () => "hi") – creator method – creates collection with “hi” 3 times
  • Custom/Complex Objects
    • var myObj = fixture.CreateMany()
    • All public properties will be created and defaulted as well
  • Autofixure respects DataAnnotations
    • Example string has [StringLength(8)] – auto fixture will create a string with at most 8 characters

Customizing Object Creation

  • fixture.Inject("LHR") – allows injection of a type of value for ALL instances
    • Everytime the fixture tries to create an anonymous string it will use LHR
    • You can also inject complex objects which would function the same way
  • var customObj = fixture.Build().Create();
    • Build gives you control over how properties are instantiated by autofixture
    • Create must be called at the end of the chain
    • .Without() – will stop a property from being auto populated
    • You can also perform actions with the “do” method added to the chain
  • Autofixture Pipeline
    • Custom Property Specimen builder – inject a builder into the pipeline to run custom code to set a property
    • fixture.Customizations.Add(new CustomSpecimenBuilder()) – this will include in the fixture pipeline

Writing Less Test Code and Improving Test Maintenance

  • AutoData attribute – Pass auto generated data into parameterized test
    • Can also create SUT by adding a parameter for it
  • InlineAutoData attribute – [InlineAutoData(-5)]
    • List the number of parameters you want to populate, all other will be populated automatically by autofixture
  • By default if an object has a dependency on an interface autofixture cannot create that object because it does not know how to inject the proper class
  • Autofixture with auto mocking nuget package
    • fixture.Customize(new AutoMoqCustomization()) – will automock interfaces
    • fixture.Freeze<Mock<ICustomObj>>() – gets reference to the automocked object for use in asserts
  • AutoMoqData – custom attribute to parameterize a test and have all setup data passed in
    • When parameterizing need to use [Frozen] attribute to get a reference to any expected mocked object