如何在工厂女孩中使用种子数据?
我正在编写一个应用程序,让用户可以在线进行测试。该测试有 2 组,每组 18 个问题,在视图上是随机的。我正在尝试编写测试以确保我在每个部分都答对了 18 个问题。我想创建一个工厂,它将访问数据库并加载随机的问题序列。知道如何做到这一点吗?我可以对文本进行硬编码,但实际上我想测试种子数据并通过不重复seeds.rb和factory.rb中的问题来干燥我的代码。
这是我所做的硬编码
Factory.define :test do |test|
test.association :user
end
Factory.define :question do |question|
question.phrase "2+2"
question.answer "4"
question.association :test
end
然后,在我的测试中,我可以写一些类似的内容:
@test = Factory(:test, :user => @user)
@question = Factory(:question, :test => @test)
有什么想法如何做到这一点?
谢谢!
I'm writing an app that let's users take a test online. The test has 2 sets of 18 questions which are randomized on the view. I'm trying to write tests to make sure that I get the correct 18 questions on each part. I'd like to create a factory that will hit the db and load in a random sequence of questions. Any idea how to do this? I can hard code the text but I actually want to test the seed data and DRY out my code by not repeating the questions in my seeds.rb and factory.rb.
Here's what I've done that is hard coded
Factory.define :test do |test|
test.association :user
end
Factory.define :question do |question|
question.phrase "2+2"
question.answer "4"
question.association :test
end
Then, in my tests, I can write something like:
@test = Factory(:test, :user => @user)
@question = Factory(:question, :test => @test)
Any ideas how to do this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果在你的spec/factories.rb 的顶部添加以下行怎么样:
如果你使用的是Rails 3.1,你可以使用以下行。
我从以下位置得到了这个答案: 我可以通过某种方式从 Rails 应用程序执行 db/seeds.rb 文件吗?
How about if at the top of your spec/factories.rb you add the following line:
If you are using, Rails 3.1, you can use the following line instead.
I got this answer from: Can I somehow execute my db/seeds.rb file from my rails app?