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

时间:2016-04-12 17:54:14

标签: ruby-on-rails-4 rspec3

型号:

 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)

我尝试了不同的方法,但仍然运气不好。任何有关这方面的帮助将不胜感激。在此先感谢!!

1 个答案:

答案 0 :(得分:0)

recent范围要求您传递限制搜索结果的参数,因此您需要相应地更改此行:

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

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