2016-04-12 34 views
0

型號:如何在rails(v4.2.5)中使用rspec(v3.4.1)測試參數化範圍方法?

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) 

我嘗試不同的方法,但仍然運氣不佳。任何幫助,將不勝感激。提前致謝!!

回答

0

recent範圍需要你傳遞限制搜索結果的參數,所以你需要相應地改變這一行:

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

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

感謝您的快速回復。它解決了我的問題。 –