2011-08-31 44 views
0

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 

如果你甚至應該不知道以這種方式測試未定義的變量,但任何幫助將是偉大的

回答

0

您可以將Effort.all方法存根。

it "tests with nil values" do 
    Effort.stub(:all).and_return(nil) 
end 

來源:http://rspec.info/documentation/mocks/stubs.htmlhttp://rspec.info/documentation/mocks/

+0

感謝兩個問題。 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

+0

存根!在RSpec2中已棄用。 2)您正在測試Effort.all返回nil時的屬性,因此將返回值保留爲nil。如果你不存在其他測試,那麼他們應該像目前一樣工作。 – Gazler