【问题标题】:How to test parameterized scope method using rspec(v3.4.1) in rails(v4.2.5)?如何在 rails(v4.2.5) 中使用 rspec(v3.4.1) 测试参数化范围方法?
【发布时间】:2016-08-03 12:02:54
【问题描述】:

型号:

 scope :recent, ->(n) {where(:created_at => 6.months.ago..Time.now).order('created_at DESC').limit(n)}  

我会在 Rspec 中做这样的事情:

before(:each) do  
  @song = double(Song,id: 1, name: "Vegan artisan.", album_id: 1, artist_id: 20, created_at: "2016-03-04 13:15:36", updated_at: "2016-03-04 13:15:36")  
end              
it "should return the recent most songs within range" do   
  Song.stub_chain(:where,:order,:limit).with(created_at:6.months.ago..Time.now).with('created_at DESC').with(2).and_return([@song,@song])  
  Song.recent.should == [@song,@song]  
end  

上述情况失败,出现以下错误:

 ArgumentError:
   wrong number of arguments (0 for 1)

我尝试了不同的方法,但还是运气不好。对此的任何帮助将不胜感激。提前致谢!!

【问题讨论】:

    标签: ruby-on-rails-4 rspec3


    【解决方案1】:

    recent范围要求你传递一个限制搜索结果的参数,因此你需要适当地改变这一行:

    Song.recent(2).should eq([@song,@song])
    

    expect(Song.recent(2)).to match_array([@song,@song])
    

    【讨论】:

      猜你喜欢
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      相关资源
      最近更新 更多