使用 AutoFixture 为字符串属性生成匿名号码
我正在对一些映射方法进行单元测试,并且我有一个字符串类型的源属性,该属性映射到整数类型的目标属性。
因此,我希望 AutoFixture 使用匿名整数为特定字符串属性(而不是所有字符串属性)创建源对象。
这可能吗?
I'm unit testing some mapping methods, and I have a source property of type string which is mapped to a destination property of type integer.
So I would like AutoFixture to create the source object with an anonymous integer for the specific string property, not for all string properties.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决此问题的最佳方法是创建一个基于约定的自定义值生成器< /a> 将匿名数值的字符串表示形式分配给特定属性,基于其名称。
因此,举个例子,假设您有一个如下所示的类:
自定义值生成器将如下所示:
这里的关键点是自定义生成器将仅针对名为
StringThatReallyIsANumber
的属性,在此case 是我们的惯例。为了在测试中使用它,您只需通过
Fixture.Customizations
集合将其添加到您的Fixture
实例即可:The best way to solve this would be to create a convention based custom value generator that assigns the string representation of an anonymous numeric value to the specific property, based on its name.
So, to give an example, assuming you have a class like this:
The custom value generator would look like this:
The key point here is that the custom generator will only target properties named
StringThatReallyIsANumber
, which in this case is our convention.In order to use it in your tests, you will simply have to add it to your
Fixture
instance through theFixture.Customizations
collection: