与未定义的变量
问题描述:
Rspec的测试复杂的代码只需要与RSpec的测试帮助小有一点有点...很新,它和不完全知道如何测试这段代码与未定义的变量
# Get a list of tasks recently used by the user
recent_task_ids = Effort.all(:select => "project_task_id",
:conditions => { :user_id => @user_id },
:group => "project_task_id",
:order => "MAX(week_commencing)")
# For each of the task ids, get the actual project task records
@recent_tasks = []
recent_task_ids.each do |effort|
if ProjectTask.find_by_id(effort.project_task_id) != nil
@recent_tasks.push(ProjectTask.find(effort.project_task_id))
end
end
如果你甚至应该不知道以这种方式测试未定义的变量,但任何帮助将是伟大的
答
您可以将Effort.all方法存根。
it "tests with nil values" do
Effort.stub(:all).and_return(nil)
end
来源:http://rspec.info/documentation/mocks/stubs.html和http://rspec.info/documentation/mocks/
感谢两个问题。 1,关于例子theres a! 。和2你放了零,我是否说得对,说出我正在寻找的属性?所以Effort.stub!(:all).and_return(:select =>“project_task_id”, :conditions => {:user_id => @user_id}, :group =>“project_task_id”, :order =>“MAX (week_commencing)“) – SD1990
存根!在RSpec2中已弃用。 2)您正在测试Effort.all返回nil时的属性,因此将返回值保留为nil。如果你不存在其他测试,那么他们应该像目前一样工作。 – Gazler