0

当我运行这个规范时,它总是给出 nil 对象,任何人都可以在这里帮助我。

 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
4

1 回答 1

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 !@songs.empty?
   respond_with @songs
  else
   render :json => {:message => "No songs found."}
  end
 end

那应该做:)

于 2013-09-23T08:31:34.447 回答