2016-05-31 70 views
0

我開始做一些測試,以我的應用程序中軌,並按照官方的教程中,我已經寫了這一點:請求在Rails進行測試沒有返回身體

class UserFlowTest < ActionDispatch::IntegrationTest 
    def login 
     post ns_login_user_path, { :user => { :username => 'user', :password => 'password' } } 
     assert_response 200 
    end 

    test "should complete a flow" do 
     login 
     post create_participant_path(:event_id => events(:myevent).id), { 
      :format  => :json, 
      :event_role => event_roles(:regular_participant).id, 
      :in_team => true 
     } 
     r = JSON.parse(response.body) 
     assert_response 200 
     puts "response creating participation #{r.as_json}" 
     participant_id = r[:participant_id] 
    end 
end 

它確實登錄OK,但後當試圖創建參與者時,response是一個沒有.body屬性的變量,只是數字200(狀態),所以JSON.parse方法崩潰。

這是我routes.rb的相關部分:

# Events 
scope 'events', :controller => :events do 
    # some routes 
    scope ':event_id', :controller => :events do 
     # some routes 
     scope 'participants', :controller => :participants do 
      post '', :action => :create_participant, :as => :create_participant 
      # some routes 
     end 
    end 
end 

而且控制器ParticipantsController.rb

class ParticipantsController < ApiController 

    before_action :require_login, :only => [:create_participant, :update_participant] 

    # Creates a participation of a person in the event 
    # Receives the following params: 
    # - +event_id+ 
    # - +in_team+::_boolean 
    # - +event_role+ 
    def create_participant 
     # … some logic 
     if participant.save 
      render :status => :ok, :json => Hash[ 
       :participant_id  => participant.id, 
       :team_participant_id => participant.team_participant_id 
      ] 
     else 
      render :status => 406, :json => Hash[ 
       :message => t('alerts.error_saving'), 
       :errors => participant.errors.as_json 
      ] 
     end 
    end 
end 

回答

0

我已經看到了控制器規格全響應對象,但是看着the code,它似乎ActionDispatch::IntegrationTest代碼僅返回response.status,而不是整個響應對象。

The documentation並不直接說你可以訪問響應對象。你可以嘗試做一個render_views,看看這是否有什麼區別,但是根據對代碼的檢查,看起來好像不會。