0

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!

spinlock
  • 3,737
  • 4
  • 35
  • 46

1 Answers1

3

How about if at the top of your spec/factories.rb you add the following line:

load(Rails.root.join("db", "seeds.rb"))

If you are using, Rails 3.1, you can use the following line instead.

Rails.application.load_seed

I got this answer from: Can I somehow execute my db/seeds.rb file from my rails app?

Community
  • 1
  • 1
e3matheus
  • 2,112
  • 1
  • 20
  • 28