gpt4 book ai didi

ruby-on-rails - 将 header 添加到 rspec get

转载 作者:数据小太阳 更新时间:2023-10-29 07:03:05 26 4
gpt4 key购买 nike

我是 rspec 的新手 - 据说!

我正在尝试将 jwt token 传递给 get 请求。我看到好几篇帖子都说语法是:

获取:端点,参数:{}, header :{}

这就是我所做的:

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

但是我得到一个错误:

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

我做错了什么?谢谢

最佳答案

不幸的是,rspec doesn't allow to set request headers ,因此您需要像这样解决它:

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

关于ruby-on-rails - 将 header 添加到 rspec get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45266027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com