使用 Rspec 和 Factory Girl 测试 before_save 回调

发布于 2025-01-01 08:39:16 字数 1650 浏览 0 评论 0原文

我很确定我在这里遗漏了一些非常基本的东西。

我想测试 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

我有以下用于 coursequestion 的工厂:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2025-01-08 08:39:16

我不会使用 Factory.create 来触发该过程。 FactoryGirl 应该用于创建测试设置,而不是触发您想要测试的实际代码。然后,您的测试将如下所示:

it 'should add lecture to question_type' do      
  course = Factory(:course, :start_time => Time.now - 1.hour, :end_time => Time.now)
  question = Factory.build(:question, :course_id => course.id, :created_at => Time.now - 10.minutes, :question_type => nil)

  question.save!                          
  question.reload.question_type.should == 'lecture'      
end

如果此测试仍然失败,您可以开始调试:

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:

it 'should add lecture to question_type' do      
  course = Factory(:course, :start_time => Time.now - 1.hour, :end_time => Time.now)
  question = Factory.build(:question, :course_id => course.id, :created_at => Time.now - 10.minutes, :question_type => nil)

  question.save!                          
  question.reload.question_type.should == 'lecture'      
end

If this test still fails, you can start debugging:

Add a puts statement inside add_type_to_question and another one inside the if statement and see what happens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文