0

AutoMoqAutoFixtureを使用して、 Mock を interfaceに生成および構成しています。このインターフェイスは、MaxLength属性を使用して各プロパティの最大長を指定します。

MaxLength生成されたモックに属性を尊重させるにはどうすればよいですか。

この属性を使用するプロパティがたくさんあることを考慮してください。(この例のように 1 つだけではありません)。

モックを作成するコードは次のとおりです。

var fixture = new Fixture();
fixture.Customize(new AutoMoqCustomization() { ConfigureMembers = true });
var my = fixture.Create<IMyClass>();

MyClass は次のとおりです。

public interface IMyClass() 
{
    [MaxLength(40)]
    public string Name { get; set; }
    ... 
    ... more properties using the MaxLenght attribute
    ...
}

次のようなカスタム標本ビルダーを作成してみました。

public class MaxLenghtSpecimenBuilder : ISpecimenBuilder
{
    public object Create(object request, ISpecimenContext context)
    {
        var pi = request as PropertyInfo;

        if (pi == null || pi.PropertyType != typeof(string))
        {
            return new NoSpecimen();
        }

        var maxLengthAttribute = (MaxLengthAttribute)Attribute.GetCustomAttribute(pi, typeof(DescriptionAttribute));

        if (maxLengthAttribute != null)
        {
            return new ConstrainedStringGenerator().Create(new ConstrainedStringRequest(maxLengthAttribute.Length), context);
        }

        return new NoSpecimen();
    }
}

を呼び出してフィクスチャに追加するにはfixture.Customizations.Add(new MaxLenghtSpecimenBuilder());

しかし、うまくいきません。カスタム属性を取得しません。常に null を返します。

4

0 に答える 0