我有一个使用帐户和用户构建的基本 Rails API。当我删除所有帐户规格时...
before_action :authenticate_user!
但是有了这个,我很难让规格通过。
# Note `json` is a custom helper to parse JSON responses
RSpec.describe 'Account API', type: :request do
# test data
let!(:user) { create(:user) }
let!(:accounts) { create_list(:account, 10, user_id: user.id) }
let(:account_id) { accounts.first.id }
# GET /accounts
describe 'GET /accounts' do
# HTTP request before examples
before do
get '/accounts'
request.headers.merge! user.create_new_auth_token
end
it 'returns accounts' do
expect(json).not_to be_empty
expect(json.size).to eq(10)
end
it 'returns status code 200' do
expect(response).to have_http_status(200)
end
end
end
如您所见,我试图在 before do 块中将身份验证令牌与此行合并...
request.headers.merge! user.create_new_auth_token
但这行不通。相反,我得到 json.size == 1 和 401 的 http_status 未经授权。