AutoFixture 使用正则表达式规则创建 属性
AutoFixture creating a property with regex rule
我最近将 this 正则表达式模式属性应用于 class 中的一个属性,以评估有效的 url 格式。现在出现了 AutoFixture 无法创建它的实例并显示错误
的问题
"AutoFixture was unable to create an instance from
Ploeh.AutoFixture.Kernel.RegularExpressionRequest, most likely because
it has no public constructor, is an abstract or non-public type."
我尝试了一些建议,例如
var contact = _fixture.Build<ContactEntity>()
.With(c=>c.Customer.Website,
new SpecimenContext(_fixture)
.Resolve(new RegularExpressionRequest(Constants.UrlRegex)))
.Create();
和
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);
}
}
但我仍然无法通过自动修复来创建 class。我是否遗漏了一些东西来创建它,或者这个正则表达式是否太复杂以至于自动修复无法处理?
我似乎已经通过使用自定义方法得到了解决方案:
_fixture = new Fixture();
_fixture.Customize<CustomerEntity>(ce =>
ce.With(x => x.Website, "http://suchTest.verwow"));
此 returns 调用客户拥有此网站(或其他正则表达式属性)的任何实例。我真的不知道 autofixture 中的某些东西是否优先于为什么这个在设置网站时有效而其他的没有。但这是一个允许我的测试工作的解决方案
您应该检查传递给 RegularExpressionRequest() 的正则表达式
new RegularExpressionRequest(Constants.UrlRegex)
正则表达式应该是生成类型而不是验证类型。例如可以如下
string TopicSuffixValidation = @"[a-zA-Z0-9]{1,5}/[a-zA-Z0-9]{1,5}"
我最近将 this 正则表达式模式属性应用于 class 中的一个属性,以评估有效的 url 格式。现在出现了 AutoFixture 无法创建它的实例并显示错误
的问题"AutoFixture was unable to create an instance from Ploeh.AutoFixture.Kernel.RegularExpressionRequest, most likely because it has no public constructor, is an abstract or non-public type."
我尝试了一些建议,例如
var contact = _fixture.Build<ContactEntity>()
.With(c=>c.Customer.Website,
new SpecimenContext(_fixture)
.Resolve(new RegularExpressionRequest(Constants.UrlRegex)))
.Create();
和
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);
}
}
但我仍然无法通过自动修复来创建 class。我是否遗漏了一些东西来创建它,或者这个正则表达式是否太复杂以至于自动修复无法处理?
我似乎已经通过使用自定义方法得到了解决方案:
_fixture = new Fixture();
_fixture.Customize<CustomerEntity>(ce =>
ce.With(x => x.Website, "http://suchTest.verwow"));
此 returns 调用客户拥有此网站(或其他正则表达式属性)的任何实例。我真的不知道 autofixture 中的某些东西是否优先于为什么这个在设置网站时有效而其他的没有。但这是一个允许我的测试工作的解决方案
您应该检查传递给 RegularExpressionRequest() 的正则表达式
new RegularExpressionRequest(Constants.UrlRegex)
正则表达式应该是生成类型而不是验证类型。例如可以如下
string TopicSuffixValidation = @"[a-zA-Z0-9]{1,5}/[a-zA-Z0-9]{1,5}"