Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 Rspec测试未定义变量的复杂代码_Ruby On Rails 3_Testing_Rspec - Fatal编程技术网

Ruby on rails 3 Rspec测试未定义变量的复杂代码

Ruby on rails 3 Rspec测试未定义变量的复杂代码,ruby-on-rails-3,testing,rspec,Ruby On Rails 3,Testing,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 },

只需要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

不确定您是否应该用这种方式测试未定义的变量,但如果有任何帮助,都会很好

您可以省去这项工作。all方法

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

资料来源:谢谢两个问题。1、示例中有一个。在你把零放在哪里,我说我把我要找的属性放在哪里,对吗?所以努力,存根!(:all).和_return(:select=>“project\u task\u id”,:conditions=>{:user\u id=>@user\u id},:group=>“project\u task\u id”,:order=>“MAX(week\u start)”)存根!在RSpec2中不推荐使用。2) 您正在测试when efforce.all的属性返回nil,因此将返回值保留为nil。如果您没有其他测试的存根,那么它们应该像当前一样工作。