jeudi 28 avril 2016

How can I use AutoFixture with internal setters?

In my assembly MyCompany.Product.Models I have a bunch of interfaces and models like this

public interface IModel : IEquatable<IModel>
{
    int MyProperty { get; }
}

public class Model : IModel
{
    int MyProperty { get; internal set; }

    public bool Equals(IModel other)
    {
        return this.MyProperty == other.MyProperty;
    }
}

In my assembly MyCompany.Product.Models.Tests I am testing the IEquatable implementations using Ploeh.AutoFixture. My AssemblyInfo.cs in the Models assembly includes

[assembly: InternalsVisibleTo("MyCompany.Product.Models.Tests")]
[assembly: InternalsVisibleTo("Ploeh.AutoFixture")]

I have a test helper that creates instances and copies and then passes them to an assertion helper.

internal static void EqualityCheck<TCheck, TEquatable>() where TCheck : class, IEquatable<TEquatable>, TEquatable
{
    // Assemble
    TCheck item = fixture.Create<TCheck>();
    TCheck copy = item.Copy();

    TCheck item2 = fixture.Create<TCheck>();
    TCheck copy2 = item2.Copy();

    // Assert
    EqualityAssert<TCheck, TEquatable>(item, copy, item2, copy2);
}

With the setters of my models specified as internal line 2 of that helper method creates objects with all properties set to null or 0. With public setters it behaves as expected. As noted above I have specified Ploeh.AutoFixture as a friend assembly.

How can I get AutoFixture to create fixtures of types with internal setters?

Aucun commentaire:

Enregistrer un commentaire