2013-09-23 29 views
0

當我運行這個規範時,它總是給零對象,任何人都可以幫我解決這個問題。如何爲api控制器的索引操作編寫rspec

require 'spec_helper' 

    describe Api::SongsController do 
    describe "GET index" do 
    it "assigns songs json" do 
     song = Song.create(:title => "song") 
     get :index, :format => :js 
     assigns(:songs).should eq([song]) 
     end 
    end   
    end 

我的控制器代碼

 def index 
     songs = Song.all 
     if !songs.empty? 
     respond_with songs 
     else 
     render :json => {:message => "No songs found."} 
     end 
    end 
+0

是什麼,你得到的錯誤?它引用了哪一行? – dax

+0

運行規範後,預期有歌曲實例但沒有。所以基本上它沒有通過測試。基本匹配測試。 –

回答

0

你的控制器應具備的實例變量。因此,改變

def index 
    songs = Song.all 
    if !songs.empty? 
    respond_with songs 
    else 
    render :json => {:message => "No songs found."} 
    end 
end 

def index 
    @songs = Song.all 
    if [email protected]? 
    respond_with @songs 
    else 
    render :json => {:message => "No songs found."} 
    end 
end 

這應該做:)

相關問題