使用 Rspec 和 Factory Girl 测试 before_save 回调
我很确定我在这里遗漏了一些非常基本的东西。
我想测试 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
我有以下用于 course
和 question
的工厂:
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
:测试一直失败,说 Question_type 为“got: nil”而不是“lecture”
因为我没有看到我的实现代码有任何明显的错误,所以我在我的开发环境中尝试了回调,它实际上可以将“lecture”添加到 Question_type。
这让我觉得我的测试可能有问题。我在这里缺少什么?默认情况下,Factory.create
是否会跳过回调?
I am pretty sure I am missing something really basic here.
I want to test if a before_save
callback does what it is supposed to do, not just that it is called.
I wrote the following test:
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
And I have the following factories for course
and question
:
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
And I wrote the following callback in my Question
model:
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
The test keeps failing saying that "got: nil" for question_type instead of 'lecture'
Since I didn't see anything obviously wrong with my implementation code, I tried the callback in my development environment and it actually worked adding 'lecture' to question_type.
This makes me think that there might be something wrong with my test. What am I missing here? Does Factory.create
skip callbacks by default?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会使用 Factory.create 来触发该过程。 FactoryGirl 应该用于创建测试设置,而不是触发您想要测试的实际代码。然后,您的测试将如下所示:
如果此测试仍然失败,您可以开始调试:
在
add_type_to_question
内添加一条 put 语句,并在if
语句内添加另一条语句,看看会发生什么。I would not use
Factory.create
to trigger the process. FactoryGirl should be used to create the test setup, not to trigger the actual code you want to test. Your test would then look like:If this test still fails, you can start debugging:
Add a puts statement inside
add_type_to_question
and another one inside theif
statement and see what happens.