我是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
但是我收到了一个错误:
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
我做错了什么?
感谢
答案 0 :(得分:9)
不幸的是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
答案 1 :(得分:0)
如果你摆脱./app4
并转而使用指定路线type: :controller
而不是photo_path
,则接受:index
。
答案 2 :(得分:0)
在rspec-rails 4.0中,我必须使用略有不同的格式添加到我的spec标头中:
request.headers.merge!(HTTP_REFERER: 'http://test.host/home')