我相信我在这里错过了一些非常基础的东西。
我想测试一个before_save
回调是否按照预期进行,而不仅仅是它是否被调用。
我编写了以下测试:
it 'should add lecture to question_type' do
@course = Factory :course,
:start_time => Time.now - 1.hour,
:end_time => Time.now
@question = Factory.create(:question,
:course_id => @course.id,
:created_at => Time.now - 10.minutes)
@question.question_type.should == 'lecture'
end
我有以下关于 课程
和 问题
的工厂函数:
Factory.define :course do |c|
c.dept_code {"HIST"}
c.course_code { Factory.next(:course_code) }
c.start_time { Time.now - 1.hour }
c.end_time { Time.now }
c.lecture_days { ["Monday", Time.now.strftime('%A'), "Friday"] }
end
Factory.define :question do |q|
q.content {"Why don't I understand this class!?"}
q.association :course
end
我在我的Question
模型中编写了以下回调函数:
before_save :add_type_to_question
protected
def add_type_to_question
@course = Course.find(self.course_id)
now = Time.now
if (time_now > lecture_start_time && time_now < lecture_end_time ) && @course.lecture_days.map{|d| d.to_i}.include?(Time.now.wday)
self.question_type = "lecture"
end
end
测试失败并显示“got: nil”,表示问题类型未定义为“lecture”。
由于实现代码没有明显的错误,我在开发环境中尝试了回调函数,它成功地将“lecture”添加到问题类型中。
这使我认为测试可能存在问题。请问我漏掉了什么?
Factory.create
默认跳过回调函数吗?