Skip to content

Instantly share code, notes, and snippets.

@shepmaster
Created October 26, 2012 13:31
Show Gist options
  • Save shepmaster/3958842 to your computer and use it in GitHub Desktop.
Save shepmaster/3958842 to your computer and use it in GitHub Desktop.
def self.get_random_quiz
quiz_ids = Quiz.all.map(&:id) # simplified to use a symbol proc
random_ids = quiz_ids.shuffle
Quiz.find(random_ids.first)
end
# Misspelled, should be "Challenge"
it "retrieves a random quiz" do
quiz_a = FactoryGirl.create(:quiz_A)
quiz_b = FactoryGirl.create(:quiz_B)
quiz_ids = [quiz_a.id, quiz_b.id] # ensure using the right ids
Quiz.stub_chain(:all, :collect).and_return(quiz_ids) # do you really need to stub this? Why not use the database?
quiz_ids.should_receive(:shuffle).and_return(quiz_ids.reverse)
QuizChalengeBuilder.get_random_quiz.should eq(quiz_b) # changed due to difference in rand
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment