将标题添加到rspec get

mar*_*tin 6 ruby rspec ruby-on-rails

我是rspec的新手 - 据说它!

我正在尝试将jwt令牌传递给get请求.我看过几篇帖子说法语是:

get :endpoint, params: {}, headers: {}

这就是我的所作所为:

require 'rails_helper'
require "rack/test"
include Rack::Test::Methods

def authenticated_header(user, password)
  response = AuthenticateUser.call(user, password)
  { "Authorization" => response.result }
end

RSpec.describe Api::AlbumsController, type: :controller do
  it "returns albums" do
    get :index, params: {}, headers: authenticated_header("admin", "123")
    puts response.body
  end
end
Run Code Online (Sandbox Code Playgroud)

但是我收到一个错误:

F.....

Failures:

  1) Api::AlbumsController returns albums
     Failure/Error: get "/index", params: {}, headers: authenticated_header("admin", "123")

     ArgumentError:
       unknown keyword: headers
     # ./spec/controllers/photos_controller_spec.rb:14:in `block (2 levels) in <top (required)>'

Finished in 0.44102 seconds (files took 8.6 seconds to load)
6 examples, 1 failure
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?谢谢

Igo*_*dov 13

不幸的是,rspec不允许设置请求标头,因此您需要像这样解决它:

  it "returns albums" do
    request.headers.merge!(authenticated_header("admin", "123"))
    get "/index", params: {}
    puts response.body
  end
Run Code Online (Sandbox Code Playgroud)