我已經重寫了默認的設計響應,它呈現json而不是html模板。當我用curl或postman測試它時,它工作正常,但是當它用rspec進行測試時。我已經閱讀了如何使用readme.md中的devise測試控制器的信息,以便它可以正常工作,但在我想測試未經身份驗證的請求時不起作用。設計重定向,而不是rspec中的json響應
響應接收到的:
<html><body>You are being <a href=\"http://test.host/users/sign_in\">redirected</a>.</body></html>
代替:
{"error":"You need to sign in or sign up before continuing."}
應用程序/控制器/ playlists_controller.rb:
class PlaylistsController < ApplicationController
before_action :authenticate_user!
def index
render json: Playlist.all
end
end
應用程序/控制器/ sessions_controller.rb:
class SessionsController < Devise::SessionsController
clear_respond_to
respond_to :json
end
規格/控制器/ playlists_controller_spec.rb:
require 'rails_helper'
describe PlaylistsController do
context 'when user is not signed in' do
it 'returns authorization error message' do
get :index
expect(response.body).to eq 'some message here'
end
end
end
規格/支持/ devise.rb
RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
end
這工作。我想知道爲什麼這個工作沒有設置內容類型或接受標題和rspec不捲曲的捲曲... – Robs
有趣。我假設你在curl中使用了內容類型json。我實際上不知道爲什麼它會使用沒有內容類型集的json – MilesStanfield
實際上沒有設置標題。它應該默認響應json,因爲它應該被重寫控制器。無論如何 - 感謝回答我:) – Robs