如何在 rails(v4.2.5) 中使用 rspec(v3.4.1) 测试参数化作用域方法?

How to test parameterized scope method using rspec(v3.4.1) in rails(v4.2.5)?

型号:

 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)

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

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

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

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