AutoFixture使用正则表达式规则创建属性

Nik*_*k M 2 c# regex autofixture

我最近将此正则表达式模式属性应用于我的类中的一个属性,以便评估有效的url格式.现在出现的问题是AutoFixture无法创建显示错误的实例

"AutoFixture无法从Ploeh.AutoFixture.Kernel.RegularExpressionRequest创建实例,很可能是因为它没有公共构造函数,是抽象或非公共类型."

我尝试了一些像

var contact = _fixture.Build<ContactEntity>()
   .With(c=>c.Customer.Website,
     new SpecimenContext(_fixture)
       .Resolve(new RegularExpressionRequest(Constants.UrlRegex)))
   .Create();
Run Code Online (Sandbox Code Playgroud)

和

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

        if (pi!=null && pi.PropertyType == typeof(string) 
            && (pi.Name.Equals("Email") || pi.Name.Equals("Website")))
        {
            //tried both of these options
            return (new OmitSpecimen() || "http://www.website.com";
        }

        return new NoSpecimen(request);
    }
}
Run Code Online (Sandbox Code Playgroud)

但我仍然无法通过自动混合来创建课程.我错过了要创建它的东西,或者这个正则表达式太复杂,无法自动处理?

Nik*_*k M 5

我似乎通过使用自定义方法得到了一个解决方案:

_fixture = new Fixture();
_fixture.Customize<CustomerEntity>(ce => 
    ce.With(x => x.Website, "http://suchTest.verwow"));
Run Code Online (Sandbox Code Playgroud)

这将返回调用客户以拥有此网站(或其他正则表达式属性)的任何实例.我真的不知道autofixture中的某些东西是否优先于为什么这个在设置网站时工作,而其他人没有.但它是一个允许我的测试工作的解决方案