lundi 1 août 2016

How to combine a Convention-based Customization with AutoFixture's [AutoData] attribute?

I am using AutoFixture's [AutoData] attribute to provide some unit tests (NUnit) with an instance of a POCO. For example:

[Test, AutoData]
public void Create_NameIsNull_ThrowsException(MyPOCO myPOCO) {..}

I've recently added a new string property to my POCO that must always contain a properly formed URL.

Naturally, [AutoData] is not aware of this requirement and generates the usual GUID-based string value for this property. This causes my tests to fail (because of some Data Annotations based validation that I have in place).

I've followed @ploeh's advice and written a Convention-based Customization for AutoFixture that specifically generates a properly formatted URL string for this new property of mine. It is a class called UrlSpecimenBuilder that implements ISpecimenProvider.

My question is, how can I combine my new ISpecimenProvider with [AutoData]?

I don't want to have to go fixture.Customizations.Add(new UrlSpecimenBuilder()); in each one of my unit test. I'm looking for a single test fixture setup step that will do the same thing.

Aucun commentaire:

Enregistrer un commentaire