I am looking for a way to speed up my Shoulda + FactoryGirl .
The model I'm trying to test ( StudentExam) has associations with other models. These related objects must exist before I can create StudentExam. For this reason they are created in setup.
However, one of our models ( School) takes considerable time to create. Since setupcalls to each operator should, the entire test case takes eons to accomplish - it creates a new @school, @student, @topicand @examfor each executable statement.
I am looking for a way to create these objects once and only once. Is there something like a method startupfor before_allthat would allow me to create entries that will be saved in the rest of the test case?
Basically I am looking for something like RSpec before (: all) . I'm not interested in the dependency problem, as these tests will never modify these expensive objects.
Here is a test case example. Apologies for the long code (I also created gist ):
class StudentExamTest < ActiveSupport::TestCase
should_belong_to :student
should_belong_to :exam
setup do
@school = Factory(:school)
@student = Factory(:student, :school => @school)
@topic = Factory(:topic, :school => @school)
@exam = Factory(:exam, :topic => @topic)
end
context "A StudentExam" do
setup do
@student_exam = Factory(:student_exam, :exam => @exam, :student => @student, :room_number => "WB 302")
end
should "take place at 'Some School'" do
assert_equal @student_exam, 'Some School'
end
should "be in_progress? when created" do
assert @student_exam.in_progress?
end
should "not be in_progress? when finish! is called" do
@student_exam.finish!
assert !@student_exam.in_progress
end
end
end
source
share