围绕已经存在的值编写工厂
当我想测试一个想法时,我一直在使用 Factory Girl 在开发中创建一些基本对象,并且通常会遇到这种情况:
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken, Login has already been taken
如果我在开发模式下运行 FactoryGirl.create :user
一次并将该用户留在数据库中,如果我在关闭控制台后再次尝试执行此操作,则必须运行该命令两次。基本上,序列在控制台实例之间重置。
有没有人想出一种方法来编写工厂,以便它们每次都会生成独特的结果?我知道我可以使用随机生成器从大域中选择一个值,从而最大限度地减少冲突的可能性。不过,如果有的话,我想找到一种更干净的方法。
I've been using Factory Girl to create some basic objects in development when I want to test out an idea, and I commonly run into this:
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken, Login has already been taken
If I've run FactoryGirl.create :user
in development mode once and left that user in the db, I'll have to run that command twice if I try to do this again after closing the console. Basically, sequences are getting reset between console instances.
Has anyone come up with a way to write factories such that they'll generate unique results each time? I'm aware that I can use random generators to pick a value from a large domain, minimizing the chance of a collision. I'd like to find a cleaner method, if available, though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为此编写一个序列。
资料来源:大约在页面的一半位置。
编辑
-阅读,您似乎正在尝试在开发模式下创建数据。
您应该为此使用 seeds.rb 文件并维护一个计数器。
You can write a sequence for this.
Source: about half way down the page.
EDIT
Upon re-reading, it seems that you are trying to create the data in development mode.
You should use the seeds.rb file for this and maintain a counter.