我正在编写一些单元测试并有一个名为Account
其中有
public Guid AccountId {get;set;}
public IEnumerable<string> EmailAddresses {get;set;}
etc...
我想使用 autofixture 创建帐户,但在获取电子邮件格式时遇到问题。
我努力了
fixture.Register<string>(() => string.Format("{0}@acme.com", fixture.Create<string>()));
但这会导致循环问题。
我可以做到这一点
fixture.Register<string>(() => string.Format("{0}@acme.com", fixture.Create<int>()));
但我宁愿在地址的开头有一个字符串。
EDIT感谢这两个答案,我在这里写了一个总结和一些其他场景 -http://nodogmablog.bryanhogan.net/2016/04/customizing-a-specific-string-inside-a-class-using-autofixture/ http://nodogmablog.bryanhogan.net/2016/04/customizing-a-specific-string-inside-a-class-using-autofixture/
有几种方法可以做到这一点。这是其中之一:
假如说MyClass
定义为
public class MyClass
{
public Guid AccountId { get; set; }
public IEnumerable<string> EmailAddresses { get; set; }
}
Then, a Fixture
对象可以像这样定制
var fixture = new Fixture();
fixture.Customize<MyClass>(c => c
.With(x =>
x.EmailAddresses,
fixture.CreateMany<MailAddress>().Select(x => x.Address)));
var result = fixture.Create<MyClass>();
所以EmailAddresses
将填充如下所示的电子邮件字符串:
"[email protected] /cdn-cgi/l/email-protection"
"[email protected] /cdn-cgi/l/email-protection"
"[email protected] /cdn-cgi/l/email-protection"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)