Ruby on rails 如何为api控制器的索引操作编写rspec

Ruby on rails 如何为api控制器的索引操作编写rspec,ruby-on-rails,ruby-on-rails-3,rspec,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3,Rspec,Ruby On Rails 3.2,当我运行这个规范时,它总是给零对象,有人能帮我吗 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).s

当我运行这个规范时,它总是给零对象,有人能帮我吗

 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

控制器应具有实例变量。所以改变

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


应该这样做:)

您得到的错误是什么?它引用的是哪一行?运行规范后,预期的是has song实例,但GET has nil。因此,基本上它没有通过测试。基本匹配测试。
 def index
  @songs = Song.all
  if !@songs.empty?
   respond_with @songs
  else
   render :json => {:message => "No songs found."}
  end
 end